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