Use TextKit to implement expressions, image and text mixing, and textkit expressions
Using coreText and coreImage that was previously provided by Xcode to achieve text-and-text mixing, the Code volume is very large and is not easy to understand,
Textkit is a new class Library launched by iOS7. It is actually an encapsulation of CoreText. TextKit does not add any new classes. It is an encapsulation of the original text display control, you can use the UILabel, UITextField, and UITextView that we like most.
// Upload the text content you want to process
NSString * str = @ "nni-hao-textkit ";
// Create NSMutableAttributedString
// This is the carrier of all TextKit, and all information will be input into NSAttributedString, and then input this String into the Text control to display it.
NSMutableAttributedString * attributed = [[NSMutableAttributedString alloc] initWithString: str];
// Set the font
// Set the font to Zapfino and 30 for all characters
[Attributed addAttribute: NSFontAttributeName value: [UIFont fontWithName: @ "Zapfino" size: 30]
Range: NSMakeRange (0, str. length)];
// Segment control. The color of the first 4 characters is set to yellow.
[Attributed addAttribute: NSForegroundColorAttributeName value: [UIColor yellowColor] range: NSMakeRange (0, 4)];
// Segment control. It must start with 3 characters including 5th, 6, and 7 characters in red.
[Attributed addattriename: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange (4, 3)];
// Create UITextView for display
UITextView * lable = [[UITextView alloc] initWithFrame: self. view. bounds];
Lable. textAlignment = UITextAlignmentCenter;
Lable. attributedText = attributed;
[Self. view addSubview: lable];
// Console display
//************************************** **************************************** ***
// Create an image attachment
NSTextAttachment * attach = [[NSTextAttachment alloc] init];
Attach. image = [UIImage imageNamed: @ "3.jpg"];
Attach. bounds = CGRectMake (0, 70, 50, 50 );
NSAttributedString * attachstring = [NSAttributedString attributedStringWithAttachment: attach];
[Attributed appendAttributedString: attachstring];
// Console display: