Common properties and methods for iOS development Uilabel

Source: Internet
Author: User

//

Viewcontroller.m

Labelall

//

#import "ViewController.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Self.view.backgroundColor = [Uicolor Yellowcolor];

UILabel *mylabel = [[UILabel alloc]initwithframe:cgrectmake (100, 100, 100, 50)];

Labele background Color

Mylabel.backgroundcolor = [Uicolor Browncolor];

[Self.view Addsubview:mylabel];

Label text

MyLabel.Text = @ "Not easy Yi";

Label text color

Mylabel.textcolor = [Uicolor Whitecolor];

Label Text Size

Mylabel.font = [Uifont systemfontofsize:14];//system default is 17

Label text alignment

mylabel.textalignment = 0;

/*

Nstextalignmentleft = 0, left

Nstextalignmentcenter = 1, medium

Nstextalignmentright = 2, right

*/

Label tag (TAG)

Mylabel.tag = 123;

Set text ellipsis

Mylabel.linebreakmode =nslinebreakbytruncatinghead;//where the Linebreakmode optional value is

/*

Linbreakmode enum{

Nslinebreakbywordwrapping = 0,//preserves the entire word, with a space as the boundary

nslinebreakbycharwrapping,//preserving entire characters

nslinebreakbyclipping,//to the border

nslinebreakbytruncatinghead,//omit the beginning, substituting the ellipsis

nslinebreakbytruncatingtail,//omit end, replace with ellipsis

nslinebreakbytruncatingmiddle//omit middle, replace with ellipsis

}

*/

Mylabel.numberoflines = 1;//number of rows is set to 1, not set when the system defaults to 1 rows

Can be implemented with numberoflines=0 when the number of rows that need to be set is unlimited

When the label size is used with the SizeToFit method, the value stored in the property is taken into account when resizing

[MyLabel SizeToFit];

Implementing multi-line text display

Mylabel.linebreakmode = nslinebreakbycharwrapping;

Mylabel.numberoflines = 0;

The Adjustfontsizetofitwidth method enables text to automatically adjust the font sizes automatically based on the label size until the text reaches the maximum minimum value of the label text size that you set, the minimum value, and the string. If you use this method, there is a big limitation that is only available when NumberOfLines is set to 1 o'clock.

Set the label's border and color

MyLabel.layer.borderColor = [Uicolor Lightgraycolor]. cgcolor;//border color, for Cgcolor

MyLabel.layer.borderWidth = 1;//Border Width

Set the label fillet

MyLabel.layer.cornerRadius = 10;

Set Shadow

Mylabel.shadowcolor =[uicolor Graycolor];

Set Shadow Size

Mylabel.shadowoffset = Cgsizemake (2.0, 2.0);

Set highlighting

Mylabel.highlighted =yes;

Set whether you can interact with users

mylabel.userinteractionenabled = YES;

Sets whether the label text is mutable

mylabel.enabled = no;//system is yes by default

#pragma Mark Adaptive Wide

UILabel *label3 = [[UILabel alloc] init];

Label3.text = @ "JFDKGIUHUHKJKLLKLMMLKGFHSRESR";

Cgsize size1 = Cgsizemake (20,20); Set a maximum line height

Nsdictionary *attribute = @{nsfontattributename:label3.font};

Cgsize labelsize = [label3.text boundingrectwithsize:size1 options:nsstringdrawingusesdevicemetrics attributes: Attribute context:nil].size;

Label3.frame = CGRectMake (0, N, labelsize.width + 3, labelsize.height);

Label3.backgroundcolor = [Uicolor Redcolor];

[Self.view Addsubview:label3];

This method of the system calculation is not particularly accurate, so to add 3-5 (without words, the string will be slightly longer than the length of the string, resulting in label.frame.size.width text display)

#pragma mark automatically adjusts the label height based on text content

NSString *text =[[nsstring Alloc]init];

Text = @ "Enter text content input text content input text content input text content";

Cgsize size = Cgsizemake (280, 180);

Uifont *fonts = [Uifont systemfontofsize:14.0];

Cgsize Msgsie = [text sizewithfont:fonts constrainedtosize:size linebreakmode:nslinebreakbycharwrapping];

UILabel *textlabel = [[UILabel alloc] init];

[Textlabel Setfont:[uifont boldsystemfontofsize:14];

Textlabel.frame = CGRectMake (20,300, 280,msgsie.height);

Textlabel.text = text;

Textlabel.backgroundcolor = [Uicolor Whitecolor];

Textlabel.linebreakmode = nslinebreakbycharwrapping;//implements text multi-line display

Textlabel.numberoflines = 0;

[Self.view Addsubview:textlabel];

#pragma mark sets the label background image

Cgsize sizeone= cgsizemake (100, 200);

UIImage *image =[uiimage imagenamed:@ "1.jpg"];

Call the method that modifies the size of the background map to match the label size

UIImage *laterimage =[self scaleimage:image Tosize:sizeone];

Uicolor * color = [Uicolor colorwithpatternimage:laterimage];

UILabel * Label1 = [[UILabel alloc] Initwithframe:cgrectmake (50, 50, 100, 200)];

[Label1 Setbackgroundcolor:color];

[Self.view Addsubview:label1];

}

-(UIImage *) Scaleimage: (UIImage *) img tosize: (cgsize) itemsize{

UIImage *i;

Create a bitmap context and set it as the context that is currently in use

Uigraphicsbeginimagecontext (itemsize);

CGRect imagerect=cgrectmake (0, 0, itemsize.width, itemsize.height);

Draw a picture that changes size

[img Drawinrect:imagerect];

Create a resized picture from the current context

I=uigraphicsgetimagefromcurrentimagecontext ();

Make the current context out of the stack

Uigraphicsendimagecontext ();

Returns the new resized image

return i;

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

@end

Common properties and methods for iOS development Uilabel

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.