Labels
Instances of the UILabel class display a read-only view that can contain one or more lines of text. For example, to create a simple label and set its text, font, textColor, and backgroundColor properties (Code Listing 4.13):
myLabel.backgroundColor = [UIColor clearColor]; myLabel.textColor = [UIColor redColor]; myLabel.font = [UIFont systemFontOfSize:18.0]; myLabel.text = @"Hello World!";
Code Listing 4.13 Creating a label.
By default, a label is rendered as black text on a white background. You can also set a font by name:
myLabel.font = [UIFont fontWithName:@"Verdana" size:18.0];
Table 4.2 shows the available fonts you can use.
Table 4.2. Fonts available on the iPhone
FAMILY |
NAME |
American Typewriter |
AmericanTypewriter, AmericanTypewriter-Bold |
AppleGothic |
AppleGothic |
Arial |
ArialMT, Arial-BoldMT, Arial-BoldItalicMT, Arial-ItalicMT |
Arial Hebrew |
ArialHebrew, ArialHebrew-Bold |
Arial Rounded MT Bold |
ArialRoundedMTBold |
Arial Unicode MS |
ArialUnicodeMS |
Courier |
Courier, Courier-BoldOblique, Courier-Oblique, Courier-Bold |
Courier New |
CourierNewPS-BoldMT, CourierNewPS-ItalicMT, CourierNewPS-BoldItalicMT, CourierNewPSMT |
DB LCD Temp |
DBLCDTempBlack |
Geeza Pro |
GeezaPro-Bold, GeezaPro |
Georgia |
Georgia-Bold, Georgia, Georgia-BoldItalic, Georgia-Italic |
Hiragino Kaku Gothic ProN |
HiraKakuProN-W6, HiraKakuProN-W3 |
Heiti J |
STHeitiJ-Medium, STHeitiJ-Light |
Heiti K |
STHeitiK-Medium, STHeitiK-Light |
Heiti SC |
STHeitiSC-Medium, STHeitiSC-Light |
Heiti TC |
STHeitiTC-Light, STHeitiTC-Medium |
Helvetica |
Helvetica-Oblique, Helvetica-BoldOblique, Helvetica, Helvetica-Bold |
Helvetica Neue |
HelveticaNeue, HelveticaNeue-Bold |
Marker Felt |
MarkerFelt-Thin |
Times New Roman |
TimesNewRomanPSMT, TimesNewRomanPS-BoldMT, TimesNewRomanPS-BoldItalicMT, TimesNewRomanPS-ItalicMT |
Thonburi |
Thonburi-Bold, Thonburi |
Trebuchet MS |
TrebuchetMS-Italic, TrebuchetMS, TrebuchetMS-BoldItalic, TrebuchetMS-Bold |
Verdana |
Verdana-Bold, Verdana-BoldItalic, Verdana, Verdana-Italic |
Zapfino |
Zapfino |
If you don't specify a font size, the label will automatically reduce the font to fit the text within the label's frame. You can control how small the font gets with the minimumFontSize property, and you can disable this behavior entirely with the adjustsFontSizeToFitWidth property.
To add a shadow to a label's text, you could write the following:
myLabel.shadowColor = [UIColor darkGrayColor]; myLabel.shadowOffset = CGSizeMake(1.0,1.0);
The shadowOffset controls set how far on the x- and y-axes from the label's text the shadow is drawn. The default is {0,-1}.
The textAlignment property allows you to align the label text to the left (the default), center, or right.
The lineBreakMode property controls how a label wraps text that is too wide to fit within its frame. You can specify whether you want the text to be word or character wrapped, clipped, or truncated at the start, end, or middle of the text.
To display multiple lines of text in a label, use the numberOfLines property and the \n newline escape character:
myLabel.numberOfLines = 2; myLabel.text = @"Hello World\nSecond line";
The height of the label's frame property needs to be tall enough to accommodate the number of lines of text you specify, or the text will be wrapped using the value defined in the lineBreakMode property (Code Listing 4.14).
Code Listing 4.14 Setting various properties of a label.