IOS: Custom uilabel

Source: Internet
Author: User

Ios ui label customizing tutorial

Http://gssdaily.com/forum/viewtopic.php? F = 21 & amp; T = 69067

By
Jack» Tue Jun 26,201 2 pm

Ios ui label customizing tutorial

Customizing a ios ui label

Here in this tutorial we will look how to customize a label. By having the knowledge of label customization you can easily customize your button on any view that we have for iPhone development.

So let us see how to customize a label:

1. Setting background
You can set a background color to your label by using "backgroundcolor" attribute of a label. For example:

Code:
Select All
UILabel *backGround = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 130, 20)];
backGround.backgroundColor = [UIColor redColor];
backGround.text = @"Red Background";


2. Setting text color
You can set color for text by using "textcolor" attribute of label. For example:

Code:
Select All
UILabel *redText = [[UILabel alloc] initWithFrame:CGRectMake(130, 80, 90, 20)];
redText.text = @"Red Color";
redText.textColor = [UIColor redColor];

3. Setting a border to your label
You can set border to your label by using "borderwidth" attribute. but for using this attribute u need to import "quartzcore/quartzcore. H ". importing this you can access layer attribute of a label. see the example below:

Code:
Select All
UILabel *borderLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 110, 130, 20)];
borderLabel.text = @"Text with border";
borderLabel.layer.borderColor = [UIColor darkGrayColor].CGColor;
borderLabel.layer.borderWidth = 2.0;

4. Setting text to the center of your label
You can set text to the center of your label by using "textalignment" attribute. For example:

Code:
Select All
UILabel *centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 140, 300, 20)];
centerLabel.text = @"Text at the center of the label";
centerLabel.textAlignment = UITextAlignmentCenter;
centerLabel.layer.borderColor = [UIColor blackColor].CGColor;
centerLabel.layer.borderWidth = 1.0;

5. Multiple Line Text
You can set multiple lines for your text within a special label. for achieving this you have to specify the number of lines you want in your label. you can do that using "numberoflines" attribute. see example given below:

Code:
Select All
UILabel *multipleLines = [[UILabel alloc] initWithFrame:CGRectMake(120, 170, 90, 40)];
multipleLines.text = @"Multiple Lines";
multipleLines.numberOfLines = 2;
multipleLines.textAlignment = UITextAlignmentCenter;
multipleLines.layer.borderColor = [UIColor blackColor].CGColor;
multipleLines.layer.borderWidth = 1.0;

6. Setting text to bold
You can set your text to bold by using "font" attribute. Example:

Code:
Select All
UILabel *boldText = [[UILabel alloc] initWithFrame:CGRectMake(130, 220, 90, 20)];
boldText.font = [UIFont boldSystemFontOfSize:15];
boldText.text = @"Bold Text";

7. Setting font to a text
Font to a text can be set using "setfont" function. See the example given below

Code:
Select All
UILabel *fontText = [[UILabel alloc] initWithFrame:CGRectMake(70, 250, 200, 20)];
[fontText setFont:[UIFont fontWithName:@"American Typewriter" size:18]];
fontText.text=@"American Typewriter";

8. Applying rounded corners to your label
Rounded corners can be applied to your label by using "cornerradius" attribute. you just have to mention the radius for the corner you want to have. see the example given below; it will clear your idea.

Code:
Select All
UILabel *roundedCorners = [[UILabel alloc] initWithFrame:CGRectMake(70, 280, 200, 20)];
roundedCorners.layer.cornerRadius = 6.0;
roundedCorners.layer.borderWidth = 1.0;
roundedCorners.textAlignment = UITextAlignmentCenter;
roundedCorners.text = @"Rounded Corners";

Thus we can apply styles to our label. Too more things can be done but these are the basic things. Please e more and enjoy.
See the snapshot given below to get an idea of what we have done in this tutorial.

New bitmap image.png (24.81 kib) viewed 262 times
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.