iOS development Uilabel for adaptive high-width

Source: Internet
Author: User

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
  1. Create Project and view controllers

    1, create a sing View application project;

    2, named for the project, the creation of engineering documents.

  2. 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.

  3. 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

  4. 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.

  5. 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

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.