富文本,NSAttributedString 与 coreText

在ios 6 以前,绘制富文本,需要重写view的drawRect 方法,使用 coreText 绘制 。ios6以后,系统为UILabel 添加了对富文本的支持。同时,对一些coreText 层需要的属性,在NS 层进行了封装,现在使用起来非常的简便,比如


<pre name="code" class="html">    NSString * countStr = @"20";
    NSString * memberCountString = [NSString stringWithFormat:@"当前%@人活跃",countStr];
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:memberCountString];
    NSRange range = [memberCountString rangeOfString:countStr];
    NSDictionary * attriDic = @{NSForegroundColorAttributeName:[UIColor redColor]};
    [attributedString addAttributes:attriDic range:range];
    self.memberCountLabel.attributedText = attributedString;

 
 
这样设置之后,显示出来的文字中,数字 “20” 将以红色显示,典型的富文本应用。而这里使用的,字体颜色的key 使用了NS层封装的 NSForegroundColorAttributeName,颜色也使用了UIColor,如果按以前的 kCTForegroundColorAttributeName,CGColor 的方式去设置,就不行了.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值