How to wrap UILabel under iOS

Source: Internet
Author: User

Transferred from: http://zasoft.blog.163.com/blog/static/205215176201301414915700/

Background:

Believe that a lot of friends have encountered, text content length is different, need to according to the content of how much to automatic line processing. Scenario: Many apps comment, some comments are long, some comments are short, some are one line, some are many lines. The following is an example of how to implement a comment. Technical points:
    1. Uilabel automatically wraps the line and adapts the frame size.
    2. UITableView High-adaptive content, adjust height in real time.
Implementation method:
    • Uilabel-Line Wrapping
    1. Gets the frame size of the Uilabel
    2. Gets the font size of the Uilabel
    3. Get the text content of Uilabel
    4. Calculates the size of the text display area based on the above 3-part data
    5. Change the Uilabel frame in real time based on the size of the 4 calculation
Code is as follows:

?

? NSString * Labelstr = @ "Hello, this is UILabel the automatic line-wrapping test content, mainly realizes the multi-line data automatic wrapping, adapts the data of the different rows number ";

? ? cgsize labelsize = {0,0};

? ? labelsize=[LabelstrSizewithfont:[Uifontsystemfontofsize: -]

? ? ? ? ? ? ? ? ? ? ? ? constrainedtosize : cgsizemake ( 200.0 " 5000 )

? ? ? ? ? ? ? ? ? ? ? ? ? ? Linebreakmode : Uilinebreakmodewordwrap ];

14 Font size for Uilabel

200 is the width of the Uilabel, and 5000 is a preset height, which indicates that within this range

?

?

l abel numberoflines = 0 Span style= "LINE-HEIGHT:28PX; Color: #000000; " > //means that label can display

on multiple lines

< Span class= "com" style= "color: #880000;" >abel linebreakmode = uilinebreakm Odecharacterwrap ;//Line break mode, consistent with the above calculation.

?? lAbel.Frame = CGRectMake(LAbel.Frame.Origin.x, LAbel.Frame.Origin.y, LAbel.Frame.size.width, labelsize.Height)///maintain the position and width of the original label, just change the height.

?

?


    • UITableView of line-height adaptive content
This is similar to the above, directly on code:

?

-(CGFloat)TableView:(UITableView*)TableView Heightforrowatindexpath:(Nsindexpath*)Indexpath

{

nsstring * labelstr = @ "Hello, this is uilabel The automatic line-wrapping test content, mainly realizes the automatic line-wrapping of multi-line data, adapts the data of different rows number ";

cgsize labelsize = {0,0};

labelsize=[LabelstrSizewithfont:[Uifontsystemfontofsize: -]

constrainedtosize : Cgsizemake (200.0,)

Linebreakmode : Uilinebreakmodewordwrap ];

14 Font size for Uilabel

200 is the width of the Uilabel, and 5000 is a preset height, which indicates that within this range


return labelsize . Height + Delta;

The delta is the height of the cell, in addition to the controls Uilabel by the adaptive control.


}

?

?

Summary: The implementation of the above 2 technical points, comments related to the app or similar app can be self-adaptive content, according to the content of how many automatic line, automatically adjust the position and size of the control to achieve the best display effect.

?????

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.