iOS开发 ☞ NSMutableAttributedString详解

原文链接地址:http://blog.csdn.net/magicZYJ/article/details/51068578
一、常用方法:
为某一范围内文字设置多个属性
setAttributes: range:
为某一范围内文字添加某个属性
addAttribute: value: range:
为某一范围内文字添加多个属性
addAttributes: range:
移除某范围内的某个属性
removeAttribute: range:

二、常见的属性及说明
字体
NSFontAttributeName
段落格式
NSParagraphStyleAttributeName

//(From stack Overflow)
NSMutableAttributedString* attrString = [NSMutableAttributedString attributedStringWithString:@"Sample text"];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:24];
[attrString addAttribute:NSParagraphStyleAttributeName
    value:style
    range:NSMakeRange(0, strLength)];
uiLabel.attributedText = attrString;

字体颜色
NSForegroundColorAttributeName
背景颜色
NSBackgroundColorAttributeName
删除线格式
NSStrikethroughStyleAttributeName
下划线格式
NSUnderlineStyleAttributeName
删除线颜色
NSStrokeColorAttributeName
删除线宽度
NSStrokeWidthAttributeName
阴影
NSShadowAttributeName

三、使用误区
来看一个使用NSMutableAttributeString最常见的Bug

-[NSConcreteAttributedString setAttributes:range:]: unrecognized selector sent to instance 0x7fbc8bded2c0

四、局限性
当我们使用NSMutableAttributedString作为属性时 ,不可以用copy,应当用strong,原因如下:
当我们使用copy时
@property (nonatomic,copy)NSMutableAttributedString *a
在我们使用这个属性时执行set方法时会进行copy之后再赋值,而copy出来的都是不可变的所以不能在使用可变方法,如果使用Strong,在set方法是直接赋值,不执行copy操作,所以当用可变的值为其赋值后,可以在使用可变的方法,所以我们在使用可变属性时采用strong。

如果我们设置了Button的attributeString属性,如果想要覆盖,只能重新设置这个属性

设置Label.AttributeText属性时 Label的numberoflines属性是无效的,必须同时给足label的高度,才可以折行显示。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值