ios:定制UILabel

iOS UI Label Customizing Tutorial

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

Postby 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
New Bitmap Image.png (24.81 KiB) Viewed 262 times
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值