一個label顯示不同顏色的字體

NSStringstring = @"模板參數【“XX點”中的時間只能輸入大寫的輸入,如要填寫11:30,則只能輸入“十一點三十分”,其余參數輸入內容不限】";
NSRange range = [string rangeOfString: @""];
NSMutableAttributedString
attribute = [[NSMutableAttributedString alloc] initWithString: string];
[attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor redColor]}range: range];
[attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor blackColor]}range: NSMakeRange(0, range.location)];
[attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor redColor]}range: NSMakeRange(range.location+ range.length, 55)];
[self.modelLabel setText: string];
[self.modelLabel setAttributedText: attribute];

![Upload 796CEFE7-1240-45AB-8978-7FB3B11BA506.png failed. Please try again.]

這樣就可以完成了,當然也可以一個label顯示多個不同顏色的字體。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容