iOS create UILabel programmatically

UILabel is basically a read only text on the screen. You can create a label programmatically using the UILabel class and set various properties such as text color, style, font size, etc. The label can be set to span more than one line and you can align the text in reference to the frame that contains the label. In this example we have added a border to display the frame that contains the label text.

Create a new Project to create an iOS application with template as Single View Application. In the Product Name and Class prefix enter "UILabel". Check the box "Use Automatic Reference Counting" (ARC) and then choose where you want to create your project.

iOS create UILabel programmatically
iOS UILabel Project

Interface file for the view controller - UILabelViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface UILabelViewController : UIViewController

@property (nonatomic, strong) UILabel *myLabel;

@end

Implementation file for the view controller - UILabelViewController.m

#import "UILabelViewController.h"

@interface UILabelViewController ()

@end

@implementation UILabelViewController

@synthesize myLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    
    //set the background color to white
    self.view.backgroundColor = [UIColor whiteColor];
    
    //create the frame that will contain our label
    CGRect labelFrame = CGRectMake(10.0f, 10.0f, 200.0f, 25.0f);
    //create the label
    self.myLabel = [[UILabel alloc] initWithFrame:labelFrame];
    //set the label text
    self.myLabel.text = @"Simple Label";
    //set the lable font
    self.myLabel.font = [UIFont boldSystemFontOfSize:16.0f];
    //se the text alignment
    self.myLabel.textAlignment =  NSTextAlignmentCenter;
    //se the border color and width
    self.myLabel.layer.borderColor = [UIColor blackColor].CGColor;
    self.myLabel.layer.borderWidth = 1.0;
    //add the label to the view
    [self.view addSubview:self.myLabel];
    
    //move the frame position
    labelFrame.origin.y += labelFrame.size.height + 10.0f;
    //increase the frame height
    labelFrame.size.height = 50.0f;
    self.myLabel = [[UILabel alloc] initWithFrame:labelFrame];
    self.myLabel.text = @"This is the first Line\nThis is the second Line";
    self.myLabel.textColor = [UIColor orangeColor];
    //set the number of lines to contain a bigger text
    self.myLabel.numberOfLines = 2;
    self.myLabel.font = [UIFont italicSystemFontOfSize:16.0f];
    self.myLabel.textAlignment =  NSTextAlignmentLeft;
    self.myLabel.layer.borderColor = [UIColor blackColor].CGColor;
    self.myLabel.layer.borderWidth = 1.0;
    [self.view addSubview:self.myLabel];
    
    labelFrame.origin.y += labelFrame.size.height + 10.0f;
    self.myLabel = [[UILabel alloc] initWithFrame:labelFrame];
    self.myLabel.textColor = [UIColor purpleColor];
    self.myLabel.text = @"This is just\nThis is just a Long\nThis is just a Long Text";
    //set the number of lines to 0, meaning use as many lines as needed
    self.myLabel.numberOfLines = 0;
    self.myLabel.font = [UIFont fontWithName:@"Arial-Bold" size:16.0f];
    self.myLabel.textAlignment =  NSTextAlignmentRight;
    self.myLabel.layer.borderColor = [UIColor blackColor].CGColor;
    self.myLabel.layer.borderWidth = 1.0;
    //Resizes and moves the receiver view so it just encloses its subviews.
    //watch the border of the frame in this example
    [self.myLabel sizeToFit];
    
    [self.view addSubview:self.myLabel];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Reference

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.