iOS-NSMutableAttributedString富文本的實現,ios富文本實現
NSMutableAttributedString繼承於NSAttributedString(帶屬性的字串)能夠簡單快速實現富文本的效果;不多說直接上和代碼,通俗易懂:
(一):
(二)代碼:
1 UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, [[UIScreen mainScreen] bounds].size.width, 30)]; 2 3 testLabel.textAlignment = NSTextAlignmentCenter; 4 5 NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"猴年大吉,新春快樂!"]; 6 7 [AttributedStr addAttribute:NSFontAttributeName 8 9 value:[UIFont systemFontOfSize:26.0]10 11 range:NSMakeRange(2, 2)];12 13 [AttributedStr addAttribute:NSForegroundColorAttributeName14 15 value:[UIColor redColor]16 17 range:NSMakeRange(2, 2)];18 19 [AttributedStr addAttribute:NSBackgroundColorAttributeName20 21 value:[UIColor redColor]22 23 range:NSMakeRange(7, 2)];24 25 testLabel.attributedText = AttributedStr;26 27 [self.view addSubview:testLabel];
(三)常見的屬性及說明:
NSFontAttributeName // 字型
NSParagraphStyleAttributeName // 段落格式
NSForegroundColorAttributeName // 字型顏色
NSBackgroundColorAttributeName // 背景顏色
NSStrikethroughStyleAttributeName // 刪除線格式
NSUnderlineStyleAttributeName //底線格式
NSStrokeColorAttributeName //刪除線顏色
NSStrokeWidthAttributeName //刪除線寬度
NSShadowAttributeName //陰影
(四)蘋果官方說明文檔:
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/index.html
(以上便是對相關知識的相關介紹和理解,還希望大家相互補充共同進步)