NSMutableAttributedString

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
      • 设置阴影属性,取值为 NSShadow 对象
    • NSTextEffectAttributeName
      • 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
    • NSBaselineOffsetAttributeName
      • 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
    • NSObliquenessAttributeName
      • 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
    • NSExpansionAttributeName
      • 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
    • NSWritingDirectionAttributeName
      • 设置文字书写方向,从左向右书写或者从右向左书写
    • NSVerticalGlyphFormAttributeName
      • 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
    • NSLinkAttributeName
      • 设置链接属性,点击后调用浏览器打开指定URL地址
    • NSAttachmentAttributeName
      • 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
    • NSParagraphStyleAttributeName
      • 设置文本段落排版格式,取值为 NSParagraphStyle 对象

2、NSMutableParagraphStyle

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
// 字体的行间距
paragraphStyle.lineSpacing = 10;
// 首行缩进
paragraphStyle.firstLineHeadIndent = 20.0f;
//(两端对齐的)文本对齐方式:(左,中,右,两端对齐,自然)
paragraphStyle.alignment = NSTextAlignmentJustified;
// 结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
// 整体缩进(首行除外)
paragraphStyle.headIndent = 20;
// 尾部缩进
paragraphStyle.tailIndent = 20;
// 最低行高
paragraphStyle.minimumLineHeight = 10;
// 最大行高
paragraphStyle.maximumLineHeight = 20;
// 段与段之间的间距
paragraphStyle.paragraphSpacing = 15;
// 段首行空白空间 /* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */
paragraphStyle.paragraphSpacingBefore = 22.0f;
// 从左到右的书写方向(一共三种)
paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
// Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height.(在受到最小和最大线高度约束之前,行高乘以该因子(如果为正)
paragraphStyle.lineHeightMultiple = 15;
// 连字属性 在iOS,唯一支持的值分别为0和1
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)];

// 实例化 label 对象
UILabel *label = [[UILabel alloc] init];
// 将 label 加到父控件上
[self.view addSubview:label];
// 设置位置尺寸
label.frame = CGRectMake(10, 40, 100, 25);
// 向 label 中添加文字
label.attributedText = attr_str;
posted @ 2022-12-01 17:07  CH520  阅读(47)  评论(0编辑  收藏  举报