Common properties and extended properties of IOS Uilabel

Source: Internet
Author: User

Objective

In the process of iOS development, Uilabel is a very common control and a large number of uses. Create a Uilabel generally need five or six code, if we need to create dozens of uilabel, it means we have to write fifty or sixty code, in fact, a lot of code is duplicated, we can write similar code in a common method to improve productivity and reduce code duplication. Some of the attributes of the official Uilabel are very limited, some of the extensibility attributes that need to be used in the development of the project, based on personal experience, are also summarized here.


First, the method of creating Uilabel public

1. The following methods are declared in the header file:

+ (UILabel *) Commonlabelwithframe: (cgrect) frame                             text: (nsstring*) Text                            color: (uicolor*) Color                             font: ( uifont*) Font                    textalignment: (nstextalignment) textalignment;

2. Implement this method in the source file:

+ (UILabel *) Commonlabelwithframe: (cgrect) frame                             text: (nsstring*) Text                            color: (uicolor*) Color                             font: ( uifont*) Font                    textalignment: (nstextalignment) textalignment{    UILabel *label = [[UILabel alloc] initWithFrame: Frame];    Label.text = text;    Label.textcolor = color;    Label.font = font;    Label.textalignment = textalignment;        Label.backgroundcolor = [Uicolor clearcolor];        return label;}


Two, dynamic setting Uilabel height

1, the first file declaration method is as follows:

/** *  Create a dynamic height of Uilabel * *  @param pointx        label's horizontal axis *  @param pointy        label's ordinate *  @param width         Label Width *  @param strcontent    content *  @param color         font Color * @param font          Font size *  @ PARAM Textalignmeng Alignment * *  @return Returns a UILabel */+ (UILabel *) Dynamicheightlabelwithpointx: (cgfloat) Pointx                                   Pointy: (cgfloat) Pointy                                    width: (cgfloat) Width                               strcontent: (NSString *) strcontent                                    color: ( Uicolor *) Color                                     font: (Uifont *) font                            Textalignmeng: (nstextalignment) Textalignmeng;

2. Implement this method in the source file:

Dynamically set label height + (UILabel *) Dynamicheightlabelwithpointx: (cgfloat) Pointx pointy: (cgfloat) Pointy width: (cgfloat) Width strcontent: (NSString *) Strcon                            Tent Color: (uicolor *) color font: (Uifont *) font Textalignmeng: (nstextalignment) textalignmeng{nsmutableparagraphstyle *paragraphstyle = [NSMut    Ableparagraphstyle Alloc]init];        Paragraphstyle.linebreakmode = nslinebreakbywordwrapping; Nsdictionary *attributes = @{nsfontattributename:font, Nsparagraphstyleattributename:paragraphstyle.        copy};                                                Cgsize labelsize = [strcontent boundingrectwithsize:cgsizemake (Width,maxfloat)                                       Options:nsstringdrawinguseslinefragmentorigin attributes:attributes         Context:nil].size;    UILabel *mylabel = [[UILabel alloc] Initwithframe:cgrectmake (Pointx, pointy, width, labelsize.height)];    [MyLabel setnumberoflines:0];    MyLabel.Text = strcontent;    Mylabel.font = font;    Mylabel.textcolor = color; return MyLabel;}

3. Test results:

-(void) viewdidload {    [super viewdidload];        NSString *str = @ "At the beginning of June, China Resources Huafa complex to 8.795 billion yuan won the Shanghai Zhabei plots, land parcel floor price of 38061 yuan/square meters, refreshed its own in March set the Shanghai Total price" Land King "record. On the same day, China Merchants Peace Union to up to 23,000 yuan/square meters of floor price, to compete for the Baoshan Big Town land, creating nearly 90% high premium rate. Not only the first-line market, Hangzhou, Suzhou and other second-tier market in the early June to focus on the ground. Hangzhou Xixi Wetland Side low-density residential land price of 9975 yuan/square meters, the premium rate of 33%, to become the 2014 Spring Festival in Hangzhou since the highest premium rate of residential land. ";       UILabel *label = [Ltlabel dynamicheightlabelwithpointx:5 pointy:20 width:self.view.frame.size.width-10 strContent:str Color:[uicolor Blackcolor] Font:[uifont systemfontofsize:20.0] textalignmeng:nstextalignmentleft];    Label.backgroundcolor = [Uicolor grouptableviewbackgroundcolor];        [Self.view Addsubview:label];    }

(1) The font size is 15th, and the margin interval is 5, the test results are as follows:

(2) The font size is 20th and the margin interval is 5, the test result is as follows:



(3) The font size is 20th and the margin interval is 50, the test result is as follows:



(4) The font size is 20th, the margin interval is 5, the text content is added, and the test results are as follows:




Third, set the alignment of the Uilabel

Some of the alignments that the authorities have provided for Uilabel are not explained here, which mainly complements the alignment that the authorities have not provided. There are three commonly used alignment options: Vertical Top, top center, top right.


1, the first file declaration method is as follows:

@interface dplabel:uilabeltypedef enum{    verticalalignmenttop = 0,//default vertical top alignment    verticalalignmentmidele,< c4/>//top center Align    Verticalalignmentbottom,  //top right align}verticalalignment; @property (nonatomic, assign) VerticalAlignment VerticalAlignment;

2, the source file implementation of the method:

#import "DpLabel.h" @implementation dplabel@synthesize verticalalignment;-(ID) initWithFrame: (cgrect) frame{self = [    Super Initwithframe:frame];    if (self) {//initialization code verticalalignment = Verticalalignmenttop; } return self;} -(VerticalAlignment) verticalalignment{return verticalalignment;}    -(void) Setverticalalignment: (verticalalignment) align{verticalalignment = align; [Self setneedsdisplay];} -(CGRect) Textrectforbounds: (cgrect) Bounds Limitedtonumberoflines: (nsinteger) numberoflines{cgrect rc = [super    Textrectforbounds:bounds Limitedtonumberoflines:numberoflines];            Switch (verticalalignment) {case VERTICALALIGNMENTTOP:RC.ORIGIN.Y = BOUNDS.ORIGIN.Y;        Break            Case VERTICALALIGNMENTBOTTOM:RC.ORIGIN.Y = bounds.origin.y + bounds.size.height-rc.size.height;        Break            DEFAULT:RC.ORIGIN.Y = Bounds.origin.y + (bounds.size.height-rc.size.height)/2;  Break  } return RC;} -(void) Drawtextinrect: (cgrect) rect{cgrect rc = [self textrectforbounds:rect limitedtonumberoflines:    Self.numberoflines]; [Super DRAWTEXTINRECT:RC];} Ways to adjust line spacing in text/* Use method * *text parameter: text content * *height parameter: line spacing * *name parameter: uilable object you use */-(void) Getlable_height:(NSString *) Tex T uiheight: (nsinteger) Height uilable: (uilabel*) name{nsmutableattributedstring *attributedstring = [[NSMutableAttrib]    Utedstring alloc] Initwithstring:text];        Nsmutableparagraphstyle *paragraphstyle = [[Nsmutableparagraphstyle alloc] init]; [Paragraphstyle setlinespacing:height];//adjust line spacing [attributedstring addattribute:nsparagraphstyleattributename value:    Paragraphstyle range:nsmakerange (0, [text length])];    Name.attributedtext = attributedstring; } @end

3. Test results

<span style= "FONT-SIZE:18PX;" >-(void) viewdidload {    [super viewdidload];        Dplabel *label = [[Dplabel alloc] Initwithframe:cgrectmake (+, +, self.view.frame.size.width-40, +)];    Label.text = @ "Test alignment";    Label.textalignment = Verticalalignmenttop;    Label.backgroundcolor = [Uicolor redcolor];    [Self.view Addsubview:label];} </span>


(1) test the vertical top alignment, the test results are as follows:


(2) test the top center alignment, the test results are as follows:


(3) test the top right alignment, the test results are as follows:


Resources: http://download.csdn.net/detail/u010545480/8808451





Common properties and extended properties of IOS Uilabel

Related Article

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.