带属性的字符串 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;





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值