Cover by Vandad Nahavandipoor

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

2.13. Displaying Static Text with UILabel

Problem

You want to display text to your users. You would also like to control the text’s font and color. A static text is text that is not directly changeable by the user at runtime.

Solution

Use the UILabel class.

Discussion

Labels are everywhere in iOS. You can see them in practically every application, except for games where the content is usually rendered with OpenGL ES instead of the core drawing frameworks in iOS. Figure 2-41 shows several labels in the Settings app on the iPhone.

Labels as titles of each one of the settings

Figure 2-41. Labels as titles of each one of the settings

You can see that the labels are displaying text in the Settings app, such as General, iCloud, Twitter, Phone, FaceTime, etc.

To create a label, instantiate an object of type UILabel. Setting or getting the text of a label can be done through its text property. So let’s define a label in our view controller’s header file:

#import <UIKit/UIKit.h>

@interface Displaying_Static_Text_with_UILabelViewController
           : UIViewController

@property (nonatomic, strong) UILabel *myLabel;

@end

Then synthesize it:

#import "Displaying_Static_Text_with_UILabelViewController.h"

@implementation Displaying_Static_Text_with_UILabelViewController

@synthesize myLabel;

...

And in the viewDidLoad, instantiate the label and tell the runtime where the label has to be positioned (through its frame property) on the view to which it will be added ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required