iOS使用NSMutableAttributedString改變字符串中部分文字的字體顏色或大小

// 創建一個文本標籤
UILabel *yyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 300, 500)];
yyLabel.numberOfLines = 0;
// 創建NSMutableAttributedString做富文本操作 -- 設置字符串
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"好好學習天天向上好好學習天天向上天天向上"];
// 改變字體大小
[attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(3, 5)];
// 改變字體顏色
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor magentaColor] range:NSMakeRange(3, 5)];
// 將改變後的字符串添加到 label 上
yyLabel.attributedText = attributedStr;
[self.view addSubview:yyLabel];

相關屬性:
設置字體:NSFontAttributeName
設置段落格式:NSParagraphStyleAttributeName
設置字體顏色:NSForegroundColorAttributeName
設置背景顏色:NSBackgroundColorAttributeName
設置刪除線格式:NSStrikethroughStyleAttributeName
設置下劃線格式:NSUnderlineStyleAttributeName
設置刪除線顏色:NSStrokeColorAttributeName
設置刪除線寬度:NSStrokeWidthAttributeName
設置陰影:NSShadowAttributeName


官方詳細文檔講解網址
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003689

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章