// 創建一個文本標籤
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