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:
- Uilabel automatically wraps the line and adapts the frame size.
- UITableView High-adaptive content, adjust height in real time.
Implementation method:
- Gets the frame size of the Uilabel
- Gets the font size of the Uilabel
- Get the text content of Uilabel
- Calculates the size of the text display area based on the above 3-part data
- 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.
?????