×

iOS UILabel


What is UILabel ?

UILabel is used to display the static Contents. For Example , "Email" , "Password" or anything. It can be single line or multiline.

Important Properties :-

1) .text
2) .font
3) .numberOfLines
4) .lineBreakMode
5) .textColor
6) .backgroundColor
7) .textAlignment

So Lets Start Creating UILabel .

Open Xcode Goto File > New >Project >Single View Application > Enter Project Name (eg :- FirstProjectViewController) > Done.

Now you see two files on left navigation Menu of Xcode , ViewController.h and ViewController.m

Grab the concepts of UILabel in iOS here in this Example.

Example :-

#import "ViewController.h"

@interface ViewController() 
{
  
}
@end

@implementation ViewController

-(void)viewDidLoad
{
   UILabel *labelEmail = [[UILabel alloc]initWithFrame:CGRectMake(20, 44,80, 24)];
    [self.view addSubview:labelEmail];
    labelEmail.text = @"Email :";
    labelEmail.textColor = [UIColor blackColor];
    labelEmail.font = [UIFont fontWithName:@"Arial" size:16.0];
    labelEmail.numberOfLines = 0;
    labelEmail.lineBreakMode = NSLineBreakByWordWrapping;
    labelEmail.layer.borderWidth = 1.0;
    labelEmail.layer.borderColor = [UIColor blackColor].CGColor;
    labelEmail.layer.cornerRadius = 3.0;
    labelEmail.textAlignment = NSTextAlignmentCenter;
}

Below Code itself is self explanatory. Firstly , we initialised a "labelEmail".Then added it to self.view and assigned it some text and given textColor with "UIColor" class and font with "UIFont" class.

NumberOfLines is used to give how much content you want for label to adjust.If you give it 0 value that means there is no Limitation of content if assigned with lineBreaking Mode. You will get these last two properties in later examples. Just get the basics here , that is how to initialise a frame to any UIElement, how to use its Properties like .text,.font,.layer,etc

.layer property

.layer property is used to give borderWidth , borderColor , cornerRadius. To give borderColor along with .layer property you have to give .CGColor at the end as you see in given Example .

Output :-