IOS simple text mixing 01, ios 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