IOS-NSMutableAttributedString Rich Text implementation, ios Rich Text implementation
NSMutableAttributedString inherits from NSAttributedString (a string with attributes), which can achieve the effect of Rich Text easily and quickly. Not to mention, NSMutableAttributedString can be directly connected to the code, which is easy to understand:
(1 ):
(2) code:
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: @ ", Happy New Year! "]; 6 7 [AttributedStr addAttribute: NSFontAttributeName 8 9 value: [UIFont systemFontOfSize: 26.0] 10 11 range: NSMakeRange (2, 2)]; 12 13 [AttributedStr addAttribute: limit 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];
(3) common attributes and descriptions:
NSFontAttributeName // font
NSParagraphStyleAttributeName // Paragraph Format
NSForegroundColorAttributeName // font color
NSBackgroundColorAttributeName // background color
NSStrikethroughStyleAttributeName // Delete line format
NSUnderlineStyleAttributeName // underline format
NSStrokeColorAttributeName // Delete line color
NSStrokeWidthAttributeName // Delete line width
NSShadowAttributeName // shadow
(4) Apple official instructions:
Https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/index.html
(The above is the introduction and understanding of relevant knowledge, and we hope you can complement each other for common progress)