ios中label富文本的设置

1.修改不同文字和颜色
// 创建一个富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@"哈哈哈哈哈123456789"]; // 修改富文本中的不同文字的样式 [attri addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5)]; [attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 5)]; // 设置数字为红色 [attri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 9)]; [attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(5, 9)];

2,添加图片

  // 添加表情
  NSTextAttachment *attch = [[NSTextAttachment alloc] init];
  // 表情图片
      attch.image = [UIImage imageNamed:@"d_aini"];
  // 设置图片大小
      attch.bounds = CGRectMake(0, 0, 32, 32);

  // 创建带有图片的富文本
      NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
  [attri appendAttributedString:string];
//
attri是上面定义的对象
// 用label的attributedText属性来使用富文本 self.textLabel.attributedText = attri;
 
posted @ 2016-01-21 20:07  codingsunny  阅读(3181)  评论(0编辑  收藏  举报