IOS simple text and text mixing 01

Source: Internet
Author: User

IOS simple text and text mixing 01

1. display the image in the Label

 

 

// Void setLabel {// NSTextAttachment-attachMent NSTextAttachment * attachMent = [[NSTextAttachment alloc] init]; // set the attachMent for the attachMent. image = [UIImage imageNamed: @ "d_aini"]; // Add the key attachMent to the image/Text butedstring * str = [NSAttributedString attributedStringWithAttachment: attachMent]; // set the label content self. label. backgroundColor = [UIColor grayColor]; self. label. attributedText = str ;}

Display Effect

 

 

 

2. Set the size of the displayed Image

 

 

// Adjust the image size in the text-and-text box-(void) setLabel2 {// NSTextAttachment-attachMent NSTextAttachment * attachMent = [[NSTextAttachment alloc] init]; // set the image attachMent. image = [UIImage imageNamed: @ "d_aini"]; // set the image size. // The image size is square. Generally, the image size is the same as the text height, not the same as the Label height CGFloat height = self. label. font. lineHeight; attachMent. bounds = CGRectMake (0, 0, height, height); // Add NSAttributedString * str = [NSAttributedString attributedStringWithAttachment: attachMent]; // set the label content self. label. backgroundColor = [UIColor grayColor]; self. label. attributedText = str ;}

Display Effect

 

 

3. insert an image into the text

 

 

// Text image mosaic display-(void) setLabel3 {// NSTextAttachment-attachment // 1. create a text attachMent containing an image and know the attachMent boundsNSTextAttachment * attachMent = [[NSTextAttachment alloc] init]; // set the image attachMent. image = [UIImage imageNamed: @ "d_aini"]; // set the size of CGFloat height = self. label. font. lineHeight; attachMent. bounds = CGRectMake (0, 0, height, height); // Add // 2. use the attachMent to create the attribute string NSAttributedString * attrString = [NSAttributedString attributedStringWithAttachment: attachMent]; // concatenate the NSString * str = @ "meters"; // 3. create a variable character concatenation string character * strM = [[initalloc] initWithString: str]; [strM appendAttributedString: attrString]; [strM appendAttributedString: [NSAttributedString alloc] initWithString: @ ""]; // sets the label content self. label. backgroundColor = [UIColor grayColor]; self. label. attributedText = strM ;}

Display Effect

 

 

 

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.