Use TextKit to implement expressions, image and text mixing, and textkit expressions

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.