
在开发中,如上图的需求我们进场会遇见,一段文字显示不同的文字效果,也许你会用比较笨的办法,将其拆分为好几个UILabel然后去做,但是那样费时费力不说还会存在适配上的问题。今天就说一个一劳永逸的方法一个UILabel解决上图效果, 所用到的知识点就是NSMutableAttributedString,其是继承于NSAttributedString(带属性的字符串)能够简单快速实现富文本的效果;
先附上上图实现的代码:
NSString * nowPriceString = @"800" ;
NSString * marketPriceString = @"999";
//先格式化字符串,将其拼接在一起
NSString *nowAndMarketPringString = [NSString stringWithFormat:@"¥%@ 市场价:%@",nowPriceString,marketPriceString] ;
//初始化NSMutableAttributedString(不建议使用NSAttributedString)
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithString:nowAndMarketPringString] ;
//设置属性(这里要注意对NSRange的使用)字体大小:NSFontAttributeName
[attrString addAttribute:NSFontAttributeName value:FONT_BOLD(28 * kFitWithWidth) range:NSMakeRange(1, nowPriceString.length)] ;
//字体前景色:NSStrikethroughColorAttributeName
[attrString addAttribute:NSForegroundColorAttributeName value:kGrayLabelColor range:NSMakeRange(1 + nowPriceString.length + 4, marketPriceString.length + 4)] ;
//删除线样式 :NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]
[attrString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(1 + nowPriceString.length + 4, marketPriceString.length + 4)] ;
//删除线颜色 :NSStrikethroughColorAttributeName
[attrString addAttribute:NSStrikethroughColorAttributeName value:kGrayLabelColor range:NSMakeRange(1 + nowPriceString.length + 4, marketPriceString.length + 4)] ;
//注意点:这里不再是给text赋值而是给label的attributedText赋值
cell.nowAndMarketLabel.attributedText = attrString ;
这样基本就可以实现上述效果。
再介绍一下NSMutableAttributedString的其他常用属性:(来自网络)
// NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
// NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色
// NSBackgroundColorAttributeName 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
// NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
// NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
// NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数)
// NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色
// NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
// NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色
// NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
// NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象
// NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象
// NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
// NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
// NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
// NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
// NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写
// NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
// NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址
// NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
// NSParagraphStyleAttributeName 设置文本段落排版格式,取值为 NSParagraphStyle 对象
简单的富文本使用就已经完成了,其实很多高级的功能也是可以用这些完成。比如图文混排效果。
如有不足欢迎大家批评指教,谢谢。共勉。。。
网友评论