最近在模仿微博的項目中發現,有的時候同一個lable會設置不同的字體和不同顏色,去創建好幾個不同的lable未免有些繁瑣,自己查閱資料以後來進行一個總結:
NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:DataArr[indexPath.row][@"OtherContent"]];
//設置需要的改變字符的顏色和range
[str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:64/255.0 green:104/255.0 blue:159/255.0 alpha:1] range:NSMakeRange(0,[self MyContentNameRange:DataArr[indexPath.row][@"OtherContent"]])];
//設置需要改變字符的字體大小和range
[str addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20.0f] range:NSMakeRange(0, 3)];
cell.OtherContent.attributedText = str;
NSMutableAttributedString應該是理解爲一個帶有屬性參數的字符串 這樣就大大的方便了開發者,可以省去很多不必要的麻煩
運行結果如下: