Uilabel is a common control for iOS development. The properties of Uilabel need to be understood, the special display effect of Uilabel also need us to grasp. Uilabel Adaptive High-width is a technical problem many beginners encounter. such as paragraph text, to let him completely branch display and to let the back of the control is not obscured. This requires that our Uilabel be able to adapt to a high width so that the contents of the text are fully displayed, and the subsequent controls can get the starting coordinates of the Uilabel and the width height to reset the frame to fit the Uilabel display without being obscured.
Tools/Materials
- MAC OS X Operating system:: OS X 10.11.5
- Compilation environment: Xcode 7.3.1
Method/Step
Create Project and view controllers
1, create a sing View application project;
2, named for the project, the creation of engineering documents.
Add Uilabel
1, create and instantiate a Uilabel in VIEWCONTROLLER.M;
2, set the background color of Uilabel;
3. Add Uilabel to the parent Class View: Addsubview.
Setting the properties of a Uilabel
1, set the maximum number of display lines (No Limit =0), adaptive needs to set to 0:
Label.numberoflines = 0;
2. Set the text content of the Text property:
Label.text = @ ""; The content needs a long point to show the effect
Adaptive mode One
Use: [Label SizeToFit];
The label width is fixed, and the height is increased by the amount of text. The fixed width here is the width=100 of the label.
Adaptive Mode Two
1. Get the text content of a text property;
2, redefine width and height;
3. Set the mode of line changing;
4. Calculate the CGRect and reset the Uilabel frame.
CGRect rect = [Label.text boundingrectwithsize:cgsizemake (self.view.frame.size.width-20, MAXFLOAT) options: nsstringdrawinguseslinefragmentorigin| Nsstringdrawingusesfontleading Attributes:@{nsfontattributename:label.font} Context:nil];
iOS development Uilabel for adaptive high-width