iOS开发笔记>> 如何渲染自定义格式字符串的UILabel

来源:互联网 发布:数据库删除一条数据 编辑:程序博客网 时间:2024/06/08 02:10

通过NSMutableAttributedString设置, 代码如下

- (void)viewDidLoad  {      [super viewDidLoad];            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"欢迎来到晟楠的博客"];      // 设置文字颜色      [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 1)];//第一个文字显示蓝色      [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(7, 2)];/最后两个文字显示红色            // 设置字体      [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(1, 3)];      self.label.attributedText = str;            //在viewdidload方法中,view刚刚创建,可能并不是真正的frame,在viewdidload中设置contentsize不太好      self.scrollView.contentSize = CGSizeMake(1000, 400);  }  


0 0
原创粉丝点击