Highly adaptive content label in ios

Source: Internet
Author: User

Highly adaptive content label in ios

The first and most important thing is to calculate the text height, and then set the label height,

Calculate the text height:

Hypothesis text:

NSString * strtest = @ "Xinhua net, Beijing, December 16" according to the Xinhua News Agency's "Xinhua International" client, "the day is in the morning ". Prime Minister Keqiang's busy schedule for the 15th day began with a breakfast. The Prime Minister of Kazakhstan, momov, came to the Beijing Tower, where Prime Minister Keqiang stayed and invited Prime Minister Keqiang to have breakfast. This is a simple breakfast with only one round table, two chairs, and some simple Chinese and Western meals. This is also a rich breakfast. The two Prime Ministers reached a wide range of consensus on Sino-Kazakhstan productivity cooperation at the dining table. This is a great breakfast. This cooperation will not only benefit the people of China and Kazakhstan, but also bring long-term common interests to countries in the region. Standing at the revolving restaurant on the 23th floor of the building, the city of Astana has a panoramic view, which is suitable for the High View. ";

In fact, ios has two methods:

Used before ios7.0:

[StrtestsizeWithFont: ContentFontconstrainedToSize: CGSizeMake (ScreenWeight-20,100 0) lineBreakMode: NSLineBreakByWordWrapping];


It is better to use the following method:

CGRect frame = [strtestboundingRectWithSize: CGSizeMake () options: outputs :@{ NSFontAttributeName: [UIFontsystemFontOfSize: 10]} context: nil];

Attributes parameters include:

NSFontAttributeName // UIFont, default Helvetica (Neue) 12

NSParagraphStyleAttributeName // NSParagraphStyle, default defaultParagraphStyle

NSForegroundColorAttributeName // UIColor, default blackColor

NSBackgroundColorAttributeName // UIColor, default nil: no background

NSLigatureAttributeName // NSNumber containing integer, default 1: default ligatures, 0: no ligatures

NSKernAttributeName // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.

NSStrikethroughStyleAttributeName; // NSNumber containing integer, default 0: no strikethrough

NSUnderlineStyleAttributeName // NSNumber containing integer, default 0: no underline

NSStrokeColorAttributeName // UIColor, default nil: same as foreground color

NSStrokeWidthAttributeName // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text wocould be 3.0)

NSShadowAttributeName; // NSShadow, default nil: no shadow

NSTextEffectAttributeName // NSString, default nil: no text effect


NSAttachmentAttributeName // NSTextAttachment, default nil

NSLinkAttributeName // NSURL (preferred) or NSString

NSBaselineOffsetAttributeName // NSNumber containing floating point value, in points; offset from baseline, default 0

NSUnderlineColorAttributeName // UIColor, default nil: same as foreground color

NSStrikethroughColorAttributeName // UIColor, default nil: same as foreground color

NSObliquenessAttributeName // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew

NSExpansionAttributeName // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion



Note the following when creating a label:

① The font of the label must be the same as the font used for computing.

② Set numberOfLines of label to zero.

③ When the label width is set to the same width as the calculated value

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.