变更记录

序号 录入时间 录入人 备注
1 2015-10-26 Alfred Jiang -
2 2015-12-21 Alfred Jiang -
2 2016-01-25 Alfred Jiang 更新富文本属性举例

方案名称

UILabel \ UITextField - 设置行间距等富文本属性

关键字

UILabel \ UITextField \ 间距 \ 行间距 \ 富文本

需求场景

  1. 需要对 UILabel / UITextField 行间距等富文本属性进行调整时

参考链接

  1. IT985 博客 - UILabel UITextField调整行间距
  2. CSDN - iOS NSMutableAttributedString/NSAttributedString 富文本设置
  3. 开源中国 - iOS 字符属性NSAttributedString描述
  4. 博客园 - 给iOS开发新手送点福利,简述文本属性Attributes的用法(推荐)

详细内容

   //  设置行间距
    - (void)setLineSpacing:(CGFloat)spacing label:(UILabel *)label
    {
        NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
        NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:spacing];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];
        [label setAttributedText:attributedString];
        [label sizeToFit];
    }
NSMutableParagraphStyle 属性举例
  • alignment //对齐方式

  • firstLineHeadIndent //首行缩进

  • headIndent //缩进

  • tailIndent //尾部缩进

  • lineBreakMode //断行方式

  • maximumLineHeight //最大行高

  • minimumLineHeight //最低行高

  • lineSpacing //行距

  • paragraphSpacing //段距

  • paragraphSpacingBefore //段首空间

  • baseWritingDirection //句子方向

  • lineHeightMultiple //可变行高,乘因数。

  • hyphenationFactor //连字符属性

NSMutableAttributedString 字符属性(Attribute)举例
  • NSFontAttributeName //(字体)该属性所对应的值是一个 UIFont 对象。该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-point Helvetica(Neue)。

  • NSParagraphStyleAttributeName //(段落)该属性所对应的值是一个 NSParagraphStyle 对象。该属性在一段文本上应用多个属性。如果不指定该属性,则默认为 NSParagraphStyle 的defaultParagraphStyle 方法返回的默认段落属性。

  • NSForegroundColorAttributeName //(字体颜色)该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色。

  • NSBackgroundColorAttributeName //(字体背景色)该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的背景颜色。如果不指定该属性,则默认无背景色。

  • NSLigatureAttributeName //(连字符)该属性所对应的值是一个 NSNumber 对象(整数)。连体字符是指某些连在一起的字符,它们采用单个的图元符号。0 表示没有连体字符。1 表示使用默认的连体字符。2表示使用所有连体符号。默认值为 1(注意,iOS 不支持值为 2)。

  • NSKernAttributeName //(字间距)该属性所对应的值是一个 NSNumber 对象(整数)。字母紧排指定了用于调整字距的像素点数。字母紧排的效果依赖于字体。值为 0 表示不使用字母紧排。默认值为0。

  • NSStrikethroughStyleAttributeName //(删除线)该属性所对应的值是一个 NSNumber 对象(整数)。该值指定是否在文字上加上删除线,该值参考“Underline Style Attributes”。默认值是NSUnderlineStyleNone。

    enum {
      NSUnderlineStyleNone = 0×00,
      NSUnderlineStyleSingle = 0×01,
      ...
    };  //设置删除线。
    
  • NSUnderlineStyleAttributeName //(下划线)该属性所对应的值是一个 NSNumber 对象(整数)。该值指定是否在文字上加上下划线,该值参考“Underline Style Attributes”。默认值是NSUnderlineStyleNone。

  • NSStrokeColorAttributeName //(边线颜色)该属性所对应的值是一个 UIColor 对象。如果该属性不指定(默认),则等同于 NSForegroundColorAttributeName。否则,指定为删除线或下划线颜色。更多细节见“Drawing attributedstrings that are both filled and stroked”。

  • NSStrokeWidthAttributeName //(边线宽度)该属性所对应的值是一个 NSNumber 对象(小数),。该值改变描边宽度(相对于字体size 的百分比)(设置笔画的粗细)。默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为3.0。

  • NSShadowAttributeName //(阴影)该属性所对应的值是一个 NSShadow 对象。设置笔画的阴影,默认为 nil。

  • NSVerticalGlyphFormAttributeName //(横竖排版)该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。

效果图

(无)

备注

results matching ""

    No results matching ""