On the use of--label in iOS development

Source: Internet
Author: User

Overview

This article describes some general uses of label. There is a lot of detail in meeting the UI requirements, so a more flexible use of the label can be done. First of all, thank Bo Master kc for his wonderful blog post.

The main contents of this article are as follows:

  1.some basic methods and properties of Labe

2.how the Label control automatically adapts to the size of the text in the control

3. design Other related classes in the label

Some basic methods and properties of Labe

As for the basic methods and properties, see the following example:

   UILabel *label = [[UILabel alloc]init];     Label.backgroundcolor = [Uicolor redcolor];     Label.text = @ "Hushunfeng";     Label.font = [Uifont systemfontofsize:12.0f];     Label.textcolor = [Uicolor Yellowcolor];

The main use of the font is to be skilled, and color is usually not simple to call this method to set.

How the Label control automatically adapts to the size of the text in the control

In practical engineering, it is usually necessary to synchronize the size of the label control according to the size of the text, so that the label control can "wrap" the text exactly, without wasting extra space, which also facilitates the layout of the view.

There are two ways to provide this functionality:

    • For display invocation of single-line text data - (cgsize) sizewithattributes: (Nsdictionary *) attrs; method to get the text width and height.
    • Display invocation for multiline text data -(CGRect) Boundingrectwithsize: (cgsize) Size options: (nsstringdrawingoptions) Options attributes: ( Nsdictionary *) Attributes Context: (Nsstringdrawingcontext *) context; method to get the width and height of the text, and note that you need to set the NumberOfLines property of the text control to 0 before this.

Let's start with a single-line example:

     UILabel *label = [[UILabel alloc]init];        Label.backgroundcolor = [Uicolor blackcolor];        Label.text = @ "Hushunfeng";        Label.font = [Uifont systemfontofsize:12.0f];        Label.textcolor = [Uicolor greencolor];        cgsize usernamesize=[label.text sizewithattributes:@{nsfontattributename: [Uifont systemFontOfSize:11]}];    Label.frame = CGRectMake (n, A, usernamesize.width, usernamesize.height);

Note the last two lines of code. we get the width and height of the font through text, and then we set the label frame.

On the use of--label in iOS development

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.