ios:定製UILabel

來源:互聯網
上載者:User

iOS UI Label Customizing Tutorial

http://gssdaily.com/forum/viewtopic.php?f=21&t=69067

by
jack » Tue Jun 26, 2012 8:49 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 particular 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. Many more things can be done but these are the basic things. Explore 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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.