iOS label auto-calculate line height

Source: Internet
Author: User

In OC, the automatic calculation of the row height of the main call system

-(--boundingrectwithsize: ( Size options: (nsstringdrawingoptions) options Attributes: (nullable nsdictionary<nsstring *, id> *) Attributes Context: ( nullable nsstringdrawingcontext *) context Span class= "S4" >ns_available (10_11, 7_0)

< Span class= "S1" > < Span class= "S4" > under different iOS systems The height of the same font is different, so you cannot set a specific height value to limit  

Workaround:

Label.text =@"Xijoaiogioaiogioioaig"; Label.numberoflines=2; Nsmutableparagraphstyle*style =[[Nsmutableparagraphstyle alloc]init]; Style.maximumlineheight= -; Style.minimumlineheight= -; CGRect rect= [Label.text boundingrectwithsize:cgsizemake (Screen_w- the, -*2) Options:nsstringdrawinguseslinefragmentorigin |nsstringdrawingusesfontleading Attributes:@{nsfontattributename:_comname.font,nsparagraphstyleattributename    : style} Context:nil]; _height= Rect.size.height;

The following is an explanation of the method

The width of the Size:label or text display, usually set to Cgsizemake (100,maxfloat) If the width is fixed to a value of 100 when automatically calculating the width height is set to Cgsizemake (maxfloat,100) If the height is fixed to a value of 100 Set to a specific value if the width and height are ok

Options

Additional options when the text is drawn.
There are four types of:
1.NSStringDrawingTruncatesLastVisibleLine
2.NSStringDrawingUsesLineFragmentOrigin
3.NSStringDrawingUsesFontLeading
4.NSStringDrawingUsesDeviceMetrics
Typically NSStringDrawingUsesLineFragmentOrigin , the NSStringDrawingUsesLineFragmentOrigin entire text calculates the size of the entire text in a rectangle that consists of each line.

Nsstringdrawingusesfontleading the spacing between the fonts (leading, line spacing: From the bottom of the lines of text to the bottom of another line of text). ) to calculate.

NSStringDrawingTruncatesLastVisibleLineOr NSStringDrawingUsesDeviceMetric , the text size is calculated in terms of each word or glyph.

Attributes: Which property of the underlying text to calculate

Context: The text display of pixels generally pass nil using the system default

Note: When you use this method, the dynamic calculation height cannot be set Adjusttosize (this method is limited to one line)



iOS label auto-calculate line height

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.