天天看点

带属性的字符串 NSMutableAttributedString/NSAttributedString

由于iOS7新出的NSTextStorge是NSMutableAttributedString的子类,所以要用好NSTextStorage,首先要学好NSMutableAttributedString和NSAttributedString。

按个人的理解,NSAttributedString是一个带有属性的字符串,通过该类可以灵活地操作和呈现多种样式的文字数据。

  alignment //对齐方式

  firstLineHeadIndent //首行缩进

  headIndent //缩进

  tailIndent  //尾部缩进

  lineBreakMode  //断行方式

  maximumLineHeight  //最大行高

  minimumLineHeight  //最低行高

  lineSpacing  //行距

  paragraphSpacing  //段距

  paragraphSpacingBefore  //段首空间

  baseWritingDirection  //句子方向

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

  hyphenationFactor //连字符属性

NSString *const NSForegroundColorAttributeName;//值为UIColor,字体颜色,默认为黑色。

NSString *const NSBackgroundColorAttributeName;//值为UIColor,字体背景色,默认没有。

NSString *const NSLigatureAttributeName;//值为整型NSNumber,连字属性,一般中文用不到,在英文中可能出现相邻字母连笔的情况。0为不连笔;1为默认连笔,也是默认值;2在ios 上不支持。

NSString *const NSKernAttributeName;//值为浮点数NSNumber,字距属性,默认值为0。

NSString *const NSStrikethroughStyleAttributeName;//值为整型NSNumber,可取值为

enum {

NSUnderlineStyleNone = 0×00,

NSUnderlineStyleSingle = 0×01,

};设置删除线。

NSString *const NSUnderlineStyleAttributeName;//同上。设置下划线。

NSString *const NSStrokeColorAttributeName;//值为UIColor,默认值为nil,设置的属性同ForegroundColor。

NSString *const NSStrokeWidthAttributeName;//值为浮点数NSNumber。设置比画的粗细。

NSString *const NSShadowAttributeName;//值为NSShadow,设置比画的阴影,默认值为nil。

NSString *const NSVerticalGlyphFormAttributeName;//值为整型NSNumber,0为水平排版的字,1为垂直排版的字。

示例代码:

//label上添加删除线

<span style="font-family:Comic Sans MS;font-size:18px;">    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    
    label.text = @"zuoyou1314";
    
    NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@"zuoyou1314"];
    [str addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt: NSUnderlineStyleSingle] range:NSMakeRange(0, str.length)];
    label.attributedText = str;
    [self.window addSubview:label];</span>
           

//设置下划线

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Some String"]; 
[attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:kCTUnderlineStyleSingle] range:(NSRange){0,[attString length]}];
 self.myLabel.attributedText = attString;