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;
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM