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;