iOS开发笔记-16:AttributedString属性相关(未解决)(文字垂直居中、下划线、中划线、划线)

参考了网上较多的文章,感觉这篇文章看起来会实用些
http://www.jianshu.com/p/d913658c2f1f

小Tips:
文字添加中划线:

NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]
问题:按上面代码添加"¥100 "会出现中划线偏移,需要用下面代码
[str setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSForegroundColorAttributeName:_kMainColor9} range:NSMakeRange(1+price.length, yuanjia.length)];

swift

nameAstr.addAttributes([NSAttributedString.Key.strikethroughStyle : NSUnderlineStyle.single.rawValue], range: NSRange(location: 0, length: 4))

变相文字垂直居中:

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineHeightMultiple = 1.3;//行高比例调整达到垂直居中
NSParagraphStyleAttributeName:paraStyle

调整文字的基线,-3是下移3,@(2)就是上移3
NSBaselineOffsetAttributeName: @(-3)

swift 下划线:

[NSAttributedString.Key.underlineStyle: NSNumber(integerLiteral: NSUnderlineStyle.single.rawValue)]

但是还是没有解决我的问题,是不是修改已设置的AttributedString属性,就是重新再设置一遍AttributedString.

比如说

UILabel *temp = [[UILabel alloc] init];
temp.text = @"颜色";
temp.textColor = [UIColor redColor];
//如果修改颜色一般是
temp.textColor = [UIColor yellowColor];

但是如果是AttributedString

NSDictionary *attrs = @{ NSFontAttributeName: [UIFont systemFontOfSize:20] ,NSForegroundColorAttributeName: [UIColor redColor]};
textNode.attributedString = [[NSAttributedString alloc]initWithString:username attributes:attrs];
[self.view  addSubiew:textNode];
//这里修改颜色,只能是
NSDictionary *attrs = @{ NSFontAttributeName: [UIFont systemFontOfSize:20] ,NSForegroundColorAttributeName: [UIColor yellowColor]};
textNode.attributedString = [[NSAttributedString alloc]initWithString:username attributes:attrs];

没有找到便捷的修改单一属性的办法,这里留个问题……

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,548评论 25 709
  • CSS格式化排版 1、字体 我们可以使用css样式为网页中的文字设置字体、字号、颜色等样式属性。下面我们来看一个例...
    张文靖同学阅读 5,093评论 0 3
  • 前言: 在iOS中,显示一段字符串通常会使用UILabel,但是它对文字的展现方式比较单一,有的时候需要展...
    饶鹏伟阅读 13,551评论 2 21
  • 2013/09/01 即将大四了,面对这个新的开始,心里很复杂,一半惶恐,一半期待。从前在新的开始前,都是断了所作...
    nutpoem阅读 3,134评论 0 0
  • 什么是格局? 格局是指一个人的眼光、胸襟、胆识等心理要素的内在布局! 《大思想的神奇》提供了一些提升格局的方法: ...
    安月白阅读 4,916评论 0 8

友情链接更多精彩内容