1.17. 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.
Note
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 1-45 shows several labels in the Settings app on the iPhone.

Figure 1-45. Labels as titles of each one of the settings
You can see that the labels are displaying text in the Settings app, such as “iCloud,” “Phone,” “FaceTime,” “Safari,” 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 first define a label in our view controller’s
implementation file:
#import "ViewController.h"@interfaceViewController()@property(nonatomic,strong)UILabel*myLabel;@end@implementationViewController...
Now in the viewDidLoad
method, 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 (in this case, our view controller’s view):
-(void)viewDidLoad{[superviewDidLoad];CGRectlabelFrame=CGRectMake(0.0f,0.0f,100.0f,23.0f);self.myLabel=
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access