iOS NSAttributeString使用详解

NSAttributeString使用详解

富文本,相当于是给string添加多条属性,如倾斜、字体颜色、间距等属性。

属性列举:
名称描述默认值
NSFontAttributeName字体 UIFontHelvetica(Neue) 12
NSForegroundColorAttributeName文字颜色 UIColorblackColor
NSBackgroundColorAttributeName背景颜色 UIColornil: no background
NSParagraphStyleAttributeName绘图的风格 NSParagraphStyledefaultParagraphStyle
NSLigatureAttributeName字符连体 NSNumber containing integerdefault 1: default ligatures, 0: no ligatures
NSKernAttributeName字符间隔 NSNumber containing floating point value, in pointsamount to modify default kerning. 0 means kerning is disabled.
NSTrackingAttributeNameiOS14新增 NSNumber containing floating point value, in pointsamount to modify default tracking. 0 means tracking is disabled.
NSStrikethroughStyleAttributeName删除线 NSNumber containing integerdefault 0: no strikethrough
NSUnderlineStyleAttributeName下划线 NSNumber containing integerdefault 0: no underline
NSStrokeColorAttributeName描绘边颜色 UIColordefault nil: same as foreground color
NSStrokeWidthAttributeName描边宽度 NSNumber containing floating point value, in percent of font point sizedefault 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)
NSShadowAttributeName阴影 NSShadowdefault nil: no shadow
NSTextEffectAttributeName文字效果 NSStringdefault nil: no text effect
NSAttachmentAttributeName附属 NSTextAttachmentdefault nil
NSLinkAttributeName链接 NSURLNSURL (preferred) or NSString
NSBaselineOffsetAttributeName基础偏移量 NSNumber containing floating point value, in pointsoffset from baseline, default 0
NSUnderlineColorAttributeName下划线颜色 UIColordefault nil: same as foreground color
NSStrikethroughColorAttributeName删除线颜色 UIColordefault nil: same as foreground color
NSObliquenessAttributeName字体倾斜 NSNumber containing floating point valueskew to be applied to glyphs, default 0: no skew
NSExpansionAttributeName字体扁平化 NSNumber containing floating point valuelog of expansion factor to be applied to glyphs, default 0: no expansion
NSWritingDirectionAttributeNameSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters. The control characters can be obtained by masking NSWritingDirection and NSWritingDirectionFormatType valuesLRE:NSWritingDirectionLeftToRight、NSWritingDirectionEmbedding, RLE:NSWritingDirectionRightToLeft 、 NSWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight、NSWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft、NSWritingDirectionOverride,
NSVerticalGlyphFormAttributeName垂直或者水平 An NSNumber containing an integer value0 means horizontal text. 1 indicates vertical text. If not specified, it could follow higher-level vertical orientation settings. Currently on iOS, it’s always horizontal. The behavior for any other value is undefined.
使用方式
NSString *priceStr = [NSString stringWithFormat:@"¥%@元", _valueLabel.text];
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:priceStr attributes:@{
            NSFontAttributeName : [UIFont systemFontOfSize:15.0],
            NSForegroundColorAttributeName : [UIColor blueColor]
}];
[attributeStr addAttributes:@{NSFontAttributeName : KFontSize(12.0f)} range:NSMakeRange(0, 1)];
[_valueLabel setAttributedText:attributeStr];
注意点:

1、NSAttributeString 在使用isEqual:方法进行对比时,对比的内容包括两方面,一是对字符串的逐个字符进行比较,二是对对应字符串的属性进行比较。

2、NSShadowAttributeName 设置阴影,单独设置不生效,必须和其他属性搭配才生效。搭配属性有:NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName


干货来了

按照官方的NSAttributeString写法写起来很复杂,尤其是多种样式的字符串还需要计算字符串range和lenth,计算错误结果就是crash。因此推荐一个三方,以类似html的写法来设置NSAttributeString。

WPAttributedMarkup

使用举例:

NSDictionary* style1 = @{@"bodys":[UIFont fontWithName:@"HelveticaNeue" size:20.0],
                             @"bold":[UIColor colorWithRed:75 / 255.0 green:212 / 255.0 blue:2 / 255.0 alpha:1],
                             @"red": [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1],
                             @"black": [UIColor colorWithRed:120 / 255.0 green:120 / 255.0 blue:120 / 255.0 alpha:1]};
NSString *str = [NSString stringWithFormat:@"<black>你的手机号:</black><bodys>%@</bodys>", phoneNumber];
self.phoneNumberLabel.attributedText = [str attributedStringWithStyleBook:style1];

可多个标签嵌套使用,实现样式的灵活设置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值