An interface element that displays static text.
- watchOS 2.0+
WKInterface to manipulate the contents of a label at runtime, such as setting a new text string. The string you specify can use the default styling you specified at design time, or you can use an attributed string to add custom styling to the text.
Do not subclass or create instances of this class yourself. Instead, define outlets in your interface controller class and connect them to the corresponding objects in your storyboard file. For example, to refer to a label object in your interface, define a property with the following syntax in your interface controller class:
During the initialization of your interface controller, WatchKit creates any needed label objects and assigns them to their connected outlets. At that point, you can use those objects to make changes to the onscreen text.
Label objects apply the font and style information specified in your storyboard. You can specify a different set of style attributes by calling the
set method and providing an appropriately formatted attributed string object. When specifying text with an
NSString object, the only other change you can make is to the text color.
Additional Information About Labels in watchOS 1
In watchOS 1, WatchKit coalesces the data from all setter method calls made during the same run loop iteration and transmits that data to the user’s Apple Watch at the end of the run loop. If you set an attribute to different values in the same run loop iteration, only the last value is transmitted. If you set an attribute to the same value in the same run loop iteration, WatchKit generates a log message so that you can track down the duplicate change.
Interface Builder Configuration Options
Xcode lets you configure information about your label interface object in your storyboard file. Table 1 lists the attributes you can configure and their meaning.
The default color of the text. You can also set this value programmatically using the
The font information to be applied to the text. You can specify one of the predefined styles or provide custom style information. For custom fonts, you must include the font in your WatchKit app bundle. You can also apply font information when using the
The amount by which the font may be scaled to accommodate text. Values must be
The technique for adjusting the position of the label’s text. When centering text vertically, you can center the text relative to the baseline or to the center of the label’s bounding box.
The alignment of the text within its bounding rectangle. Use this attribute to align text when the width of the label is greater than the width of the text itself.
The maximum number of lines to allow for the label text. Text that does not fit on the specified number of lines is truncated.