NSAttributedString

属性字符串,子类有NSMutableAttributedString,比较常用

常用的属性有:

NSFontAttributeName 字号,默认12号字体

NSParagraphStyleAttributedName 换行类型

NSForegroundColorAttributeName 前景色

NSBackgroundColorAttributeName 背景色

NSLigatureAttributeName 连字符,0代表无连字符,1代表默认连字符,2在iOS上不可用,连字符跟文字和字体有关,比如在英文中,“fl”和“fi”在“Hoefler Text”字体中会连在一起,其它的文字和字体(比如默认的Helvetica Neue)则不行


图1 连字符效果

NSKernAttributeName 突出,float类型的NSNumber

NSStrikethroughStyleAttributeName 删除线类型,同下划线类型NSUnderlineStyle相似,integer类型的NSNumber

NSStrikethroughColorAttributeName 删除线颜色

NSUnderlineStyleAttributeName 下划线类型,NSUnderlineStyle,integer类型的NSNumber,类型如下

    NSUnderlineStyleNone, #无
    NSUnderlineStyleSingle, #单线
    NSUnderlineStyleThick, #粗线
    NSUnderlineStyleDouble, #双线,可与NSUnderlineStyleThick配合使用

#下面的属性,需要和NSUnderlineStyle配合着使用,如NSUnderlineStyleSingle | NSUnderlinePatternDot
    NSUnderlinePatternSolid, #默认实线
    NSUnderlinePatternDot, #点类型虚线
    NSUnderlinePatternDash, #破折号类型虚线
    NSUnderlinePatternDashDot, #破折号-点类型的虚线
    NSUnderlinePatternDashDotDot, #破折号-点-点 类型的虚线

NSUnderlineColorAttributeName 下划线颜色

NSStrokeColorAttributeName 描边颜色

NSStrokeWidthAttributeName 描边宽度,float类型的NSNumber

NSShadowAttributeName 阴影,值是NSShadow,可设置偏移量,颜色等

NSTextEffectAttributeName 文字效果 值有NSTextEffectLetterpressStyle,印刷类型

NSObliquenessAttributeName 倾斜度,float类型的NSNumber

NSAttachmentAttributeName 附件属性,只能在文本末尾添加,不能使用到[poemString addAttributes:<#(nonnull NSDictionary*)#> range:<#(NSRange)#>]中去(即使放进去也没有效果),使用方法如下:


NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];

textAttachment.image = [UIImage imageNamed:@"bill_receiveReward@2x.png"];

textAttachment.bounds = CGRectMake(0, 0, 30, 30);

NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];

[poemString appendAttributedString:imageStr];

NSLinkAttributeName 设置链接,会调用Safari浏览器,在UILabel上面使用没有效果,在UITextView上使用,需禁止编辑,然后才能调用Safari浏览器,也可以实现UITextView的代理 方法进行拦截
NSBaselineOffsetAttributeName 基准偏移度,正值往上偏,负值下偏
NSExpansionAttributeName 横向拉伸属性,正值横向拉伸,负值横向压缩
NSWritingDirectionAttributeName 写作方向,NSNumber类型的NSArray对象,

0 LRE NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding
1 RLE NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding
#由于没有重写,故只会变化最后的标点符号
2 LRO NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride
3 RLO NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride

NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义(如果设置为1,只有标点符号起作用)

Currently on iOS, it's always horizontal.  The behavior for any other value is undefined.

Demo见https://github.com/hengyizhangcn/NSAttributedStringDemo.git

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容