TIPS: how to display images in a Label,
There are actually some requirements. For example, the information sent on the QQ chat interface can be displayed using a label (or a button), but sometimes users may send images. If you can make the Label display text when it encounters text, you can display the image when it encounters an image. Some may say that Quartz2D is used, and the context is directly used in the drawinrect of the Label. However, this is too deliberate and troublesome.
If you did not see this article in Dong Boran's blog, click to view the original article.
We recommend that you use the attributes of the Label system.
UIImage * image = [UIImage imageNamed: @ "321.jpg"]; // 1> Generate the text attachment NSTextAttachment * textAttach = [[NSTextAttachment alloc] init]; textAttach. image = image; // 2> use a text attachment to create an attribute text NSAttributedString * strA = [NSAttributedString attributedStringWithAttachment: textAttach]; self. lblChat. attributedText = strA;
LblChat is a member variable that is dragged to a label in the storyboard.
Everyone knows label. Text, but not all about label. attributedText.
Assign values to the label attribute text using text attachments.
Comparison before and after a value assignment (if you want to make the size as determined by yourself, you need to increase the judgment and add constraints)