1、Key值介绍
1.1 详细介绍
NSFontAttributeName
字体:该属性所对应的值是一个 UIFont 对象。该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-point Helvetica(Neue)。
NSParagraphStyleAttributeName
段落格式:该属性所对应的值是一个 NSParagraphStyle 对象。该属性在一段文本上应用多个属性。
如果不指定该属性,则默认为 NSParagraphStyle 的defaultParagraphStyle 方法返回的默认段落属性。
NSForegroundColorAttributeName
字体颜色:该属性所对应的值是一个 UIColor 对象。
该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色。
其设置的颜色与 UILabel 的 textColor 属性设置的颜色在地位上是相等的,与 NSBackgroundColorAttributeName 地位上也相等,谁最后赋值,最终显示的就是谁的颜色,但是textColor属性可以与 NSBackgroundColorAttributeName 属性可叠加。
NSBackgroundColorAttributeName
背景颜色:该属性所对应的值是一个 UIColor 对象。
该属性用于指定一段文本的背景颜色。如果不指定该属性,则默认无背景色。
NSStrikethroughStyleAttributeName
删除线格式 :设置删除线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值:
NSUnderlineStyleNone 不设置删除线
NSUnderlineStyleSingle 设置删除线为细单实线
NSUnderlineStyleThick 设置删除线为粗单实线
NSUnderlineStyleDouble 设置删除线为细双实线
虽然使用了枚举常量,但是枚举常量的本质仍为整数,所以同样必须先转化为 NSNumber 才能使用
删除线和下划线使用相同的枚举常量作为其属性值
目前iOS中只有上面列出的4中效果,虽然我们能够在头文件中发现其他更多的取值,但是使用后没有任何效果
另外,删除线属性取值除了上面的4种外,其实还可以取其他整数值,有兴趣的可以自行试验,取值为 0 - 7时,效果为单实线,随着值得增加,单实线逐渐变粗,取值为 9 - 15时,效果为双实线,取值越大,双实线越粗。
NSUnderlineStyleAttributeName
下划线格式:该属性所对应的值是一个 NSNumber 对象(整数)。
该值指定是否在文字上加上下划线,该值参考“Underline Style Attributes”。默认值是NSUnderlineStyleNone。
下划线除了线条位置和删除线不同外,其他的都可以完全参照删除线设置。
NSStrikethroughColorAttributeName
删除线颜色:设置删除线颜色,取值为 UIColor 对象,默认值为黑色
NSStrokeWidthAttributeName
NSShadowAttributeName
阴影:该属性所对应的值是一个 NSShadow 对象。默认为 nil。
单独设置不好使,和这三个任意一个都可以,NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName
NSKernAttributeName(字间距)
字间距:NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
边线颜色:NSStrokeColorAttributeName(边线颜色) 和 NSStrokeWidthAttributeName(边线宽度)
NSStrokeWidthAttributeName 这个属性所对应的值是一个 NSNumber 对象(小数)。该值改变笔画宽度(相对于字体 size 的百分比),负值填充效果,正值中空效果,默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为 3.0。
同时设置了空心的两个属性,并且 NSStrokeWidthAttributeName 属性设置为整数,文字前景色就无效果了
NSObliquenessAttributeName
NSExpansionAttributeName
1.2 具体字段
NSFontAttributeName
设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
NSForegroundColorAttributeName
设置字体颜色,取值为 UIColor对象,默认值为黑色
NSBackgroundColorAttributeName
设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
NSLigatureAttributeName
设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
NSKernAttributeName
设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
NSStrikethroughStyleAttributeName
设置删除线,取值为 NSNumber 对象(整数)
NSStrikethroughColorAttributeName
设置删除线颜色,取值为 UIColor 对象,默认值为黑色
NSUnderlineStyleAttributeName
设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
NSUnderlineColorAttributeName
设置下划线颜色,取值为 UIColor 对象,默认值为黑色
NSStrokeWidthAttributeName
设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
NSStrokeColorAttributeName
填充部分颜色,不是字体颜色,取值为 UIColor 对象
NSShadowAttributeName
NSTextEffectAttributeName
设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
NSBaselineOffsetAttributeName
设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
NSObliquenessAttributeName
设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
NSExpansionAttributeName
设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
NSWritingDirectionAttributeName
NSVerticalGlyphFormAttributeName
设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
NSLinkAttributeName
NSAttachmentAttributeName
设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
NSParagraphStyleAttributeName
设置文本段落排版格式,取值为 NSParagraphStyle 对象
2、NSMutableParagraphStyle
复制 NSMutableParagraphStyle * paragraphStyle = [ [ NSMutableParagraphStyle alloc] init ] ;
paragraphStyle. lineSpacing = 10 ;
paragraphStyle. firstLineHeadIndent = 20 . 0f;
paragraphStyle. alignment = NSTextAlignmentJustified ;
paragraphStyle. lineBreakMode = NSLineBreakByTruncatingTail ;
paragraphStyle. headIndent = 20 ;
paragraphStyle. tailIndent = 20 ;
paragraphStyle. minimumLineHeight = 10 ;
paragraphStyle. maximumLineHeight = 20 ;
paragraphStyle. paragraphSpacing = 15 ;
paragraphStyle. paragraphSpacingBefore = 22 . 0f;
paragraphStyle. baseWritingDirection = NSWritingDirectionLeftToRight ;
paragraphStyle. lineHeightMultiple = 15 ;
paragraphStyle. hyphenationFactor = 1 ;
3、简单实践
NSString * demo_info = @"莫等闲,白了少年头,空悲切。" ;
NSMutableAttributedString * attr_str = [ [ NSMutableAttributedString alloc] initWithString: demo_info] ;
NSMutableParagraphStyle * paragraphStyle = [ [ NSMutableParagraphStyle alloc] init ] ;
paragraphStyle. lineSpacing = 2 ;
paragraphStyle. alignment = NSTextAlignmentCenter ;
NSDictionary * attributes = @{ NSFontAttributeName : [ UIFont boldSystemFontOfSize : 16 ] ,
NSForegroundColorAttributeName : [ UIColor redColor] ] ,
NSParagraphStyleAttributeName : paragraphStyle
} ;
[ attr_str addAttributes: attributes
range: NSMakeRange ( 0 , value. length) ] ;
UILabel * label = [ [ UILabel alloc] init ] ;
[ self . view addSubview: label] ;
label. frame = CGRectMake ( 10 , 40 , 100 , 25 ) ;
label. attributedText = attr_str;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?