Creating programmatically a label (UILabel)

May 8th, 2009 by Chris Leave a reply »

UILabel allows you to display a single line text, most of us think so. In fact it can be used to display multiple lines as well.

To create an UILabel programmaticaly, code:

	UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
To display a text within UILabel use:
	myLabel.text = @"Lorem...";

And of course to add UILabel to your view:

	[self.view addSubview:myLabel];

By default UILabel has a white background, you can remove the background or change the color easily:

	myLabel.backgroundColor = [UIColor clearColor]; // [UIColor brownColor];

Setting the font:

	myLabel.font = [UIFont fontWithName:@"Zapfino" size: 14.0];

You can easily get a list of available fonts and the preview of them, using the free application Fonts! by AppEngines. UILabel can display text in different color then black and shadows as well:

	myLabel.shadowColor = [UIColor grayColor];
	myLabel.shadowOffset = CGSizeMake(1,1);
	myLabel.textColor = [UIColor blueColor];

Best effect you will obtain by setting a color using RGB while setting the alpha half transparent. shadowOffset gives you the ability to set the shadow’s angle.

By default text is horizontally aligned to the left, you can set it either to the right or to the center.

	myLabel.textAlignment = UITextAlignmentRight; // UITextAlignmentCenter, UITextAlignmentLeft

Text wrapping

As I told you, UILabel can display multiple lines of text, and to achieve it you need to set two properties lineBreakMode and numberOfLines:

	myLabel.lineBreakMode = UILineBreakModeWordWrap;
	myLabel.numberOfLines = 2; // 2 lines ; 0 - dynamical number of lines
	myLabel.text = @"Lorem ipsum dolor sit\namet...";

That’s all about UILabel. More can be found in UILabel Class Reference in documentation.

Advertisement

9 comments

  1. akp says:

    Hello Chris

    How to display contents in UIlabels.
    I want to display contents of a book in UILabels. How to do paging and what should be the approach in doing that.
    any sample code or help?

  2. Celia says:

    Hi Chris –

    I have a very simple program consisting of a UILabel in a UIView that isn’t working properly. For some reason the text of the label isn’t being displayed. I was hoping you could help. Thanks!

    – Celia

    Here’s the code:
    (Finder 10.5.8, XCode 3.1.4, iPhone Simulator 3.1)

    // HelloSimpleAppDelegate.h

    #import

    @interface HelloSimpleAppDelegate : NSObject {
    UIWindow *window;
    UIView *view;
    UILabel *label;
    }

    @property (nonatomic, retain) IBOutlet UIWindow *window;
    @property (nonatomic, retain) IBOutlet UIView *view;
    @property (nonatomic, retain) IBOutlet UILabel *label;

    @end

    // HelloSimpleAppDelegate.m

    #import “HelloSimpleAppDelegate.h”

    @implementation HelloSimpleAppDelegate

    @synthesize window;
    @synthesize view;
    @synthesize label;

    - (void)applicationDidFinishLaunchingUIApplication *)application {
    CGRect screenBounds = [ [ UIScreen mainScreen ] applicationFrame ];

    self.window = [ [ UIWindow alloc ] initWithFrame: screenBounds ];
    view = [ [ UITextView alloc ] initWithFrame: screenBounds ];
    CGRect rect = CGRectMake(50, 100, 200, 100);
    label = [ [ UILabel alloc ] initWithFrame:rect];
    [label setText: @"Hello Simple"];

    [window addSubview: view];
    [view addSubview: label];
    [window makeKeyAndVisible];
    }

    - (void)dealloc {
    [window release];
    [view release];
    [super dealloc];
    }

    @end

    // main.m

    #import

    int main(int argc, char *argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
    }

    • Celia says:

      Here’s some more information: when I click on the Home button, the text is displayed briefly.

      • Celia says:

        Here’s some more information: it works on another computer (i.e. the text is displayed at the start) that is running Mac OS 0.5.7, XCode 3.1.1, iPhone Simuator 1.0. Deep sigh.

        p.s. the “#import” in HelloSimpleAppDelegate.h should be “#import “.

        • Celia says:

          The problem was that I needed to remove all the extra stuff Interface Builder puts in your app directory by default. IB was adding an extra layer on top of my program and it was hiding it.

  3. Mark says:

    Hi,

    I am looking to add some text to one of my UIViews. The sort of things are Phone Number, Opening Times, and a general Description.

    Is it possible to add all these items with one UILabel, but with certain parts shown in Bold?

    For example: Phone: (in bold) +44 (xxx) xxxxxx (in normal)

    • Chris says:

      I’m afraid you can’t. UILabel cannot provide you rich text editing features. You will have to use either few UILabels or images.

      regards
      Chris

  4. Josh says:

    I am getting this error: request for member ‘view’ in something not a structure or union at [self.view addSubview:myLabel];

    What am i Missing here?

    • Chris says:

      Hi Josh!
      You can use self.view usually if you are using the UIViewController, I guess you are using another class, try downloading some of my sample projects from the other tutorials and you will see the difference, I’m pretty sure, that insted of UIViewController, you used UIView.

Leave a Reply