富文本,NSAttributedString 与 coreText

来源:互联网 发布:gps漂移过滤算法 源码 编辑:程序博客网 时间:2024/05/16 11:16

在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
原创粉丝点击