Text and Category labels
To display text in the Watch app, use the Tag object. The category label supports formatted text that can be modified by the program at run time.
To add a label to the interface controller, you can drag it to the corresponding storyboard scene (storyboard), where you specify the initial text string and format for the tag. WatchKit supports both standard fonts as well as custom fonts. Figure 8-1 shows the standard font style you can use.
Figure 8-1 Standard font style for labels
For more information on configuring label objects, refer to wkinterfacelabel Class Reference.
Using custom Fonts to customize fonts
In addition to the standard font style, you can customize the font of formatted strings. Customize the font as follows:
· Import custom font files in the Watch app and in the WatchKit expansion pack.
· Add the __uiappfonts__ key to the Info.plist file in your Watch app and use this to specify the fonts that are added to the package. For more information on this key, please refer to Information property List key Reference
Note: You must import fonts in the WatchKit extension to create a string of the specified font at run time. When sent to Apple Watch, the font information contains the attribute string, and the font copy in the Watch app package will render the font.
Use a custom font to format text, use font information to create an attributed string, and use that string as the text of your label, as shown in Listing 8-1. The name and size of the font are encoded by the property string, which allows you to update the label of the user's Apple Watch.
A string that uses a custom font as a label
12345678 |
// Configure an attributed string with custom font information.
UIFont* menloFont = [UIFont fontWithName:@
"Menlo"
size:12.0];
NSAttributedString *attrString = [[NSAttributedString alloc]
initWithString:@
"My Formatted Text"
attributes:@{NSFontAttributeName: menloFont}];
// Set the text on the label object.
[self.myCustomFontLabel setAttributedText:attrString];
|
Customizing the System font customization
Custom system fonts, using the uifontdescriptor object to specify a new system-based font. The example in listing 8-2 shows how to customize a standard system font that uses only lowercase characters. After converting the system font to a font descriptor, add the __klowercasetype__ and __klowercasesmallcapsselector__ properties (in the core Text defined in the framework) and then the result of the font descriptor to create a new Font object.
Specifies that the system font is lowercase
12345678910 |
CGFloat fontSize = 18.0;
UIFont *afont = [UIFont systemFontOfSize:fontSize];
UIFontDescriptor *fontDescriptor =
[[afont fontDescriptor] fontDescriptorByAddingAttributes:@{
UIFontDescriptorFeatureSettingsAttribute : @[
@{ UIFontFeatureTypeIdentifierKey : @(kLowerCaseType),
UIFontFeatureSelectorIdentifierKey : @(kLowerCaseSmallCapsSelector) },],
}];
UIFont *smallCapFont = [UIFont fontWithDescriptor:fontDescriptor size:fontSize];
|
Internationalization of your text code
Watch apps can use the internationalization technology approach that iOS apps has figured out.
· Use storyboard and Xib files that are based on Xcode internationalization support. Internationalization-based allows you to support all localization with just one storyboad file. Localized strings exist in a specific regional language string, respectively.
· Use the macro definition of the nslocalizedstring family to have the program automatically retrieve localized strings.
· Use the Nsnumberformatter class to format numeric values using the user locale and local settings.
· Use the NSDateFormatter class to format time by using the user's region and local settings.
When it's time to internationalize your app, your main consideration is how to tweak the interface so that the list of tags (and other text controls) is enough to hold. For example, the horizontal arrangement of three buttons, a better vertical arrangement can give each label text to provide a longer space to accommodate.
WatchKit Programming Guide: Watch apps--text, tags, and pictures