TIPS: how to display images in a Label,

Source: Internet
Author: User

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)

 

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.