WatchKit WKInterfaceLabel不能更改字体 [英] WatchKit WKInterfaceLabel can't change font
问题描述
目前您无法在WatchKit中使用随附的iOS字体。唯一可用的是系统(旧金山)。
包含自定义字体同时在WatchKit应用程序和WatchKit
扩展程序包中生成文件。
将应用程序(UIAppFonts)键提供的字体添加到 WatchKit应用程序和 WatchKit Extension Info.plist 文件
将此代码添加到 awakeWithContext
知道正确的字体名称,以便在以后的代码中调用:
pre $ print $(自定义字体名称)
print (UIFont.fontNames(forFamilyName:Exo))
print(UIFont.fontNames(forFamilyName:Tabardo))
运行应用程序并记下打印到调试控制台的字体名称。一旦你知道了正确的名字,你可以添加这个代码在你的WatchKit扩展的某个地方:
$ b $ pre $ code var fontSize = CGFloat(32)
var text =so cool
var cstmFont = UIFont(name:Tabardo,size:fontSize)!
var attrStr = NSAttributedString(string:text,attributes:
[NSFontAttributeName:cstmFont])
firstLabel.setAttributedText(attrStr)
$ b $ fontSize = CGFloat(36)
text =right on!
cstmFont = UIFont(名称:Exo-Regular,size:fontSize)!
attrStr = NSAttributedString(string:text,attributes:
[NSFontAttributeName:cstmFont])
secondLabel.setAttributedText(attrStr)
$ p $
享受手表上的自定义字体! 使用自定义字体的手表界面>
请注意,浏览和通知无法使用自定义字体。如果你想在那里使用一个,你将不得不使用渲染的图像。但是,因为浏览和通知应该加载得很快,所以你希望在被调用的时候准备好映像。
I'm trying to change label font. But every font I set in the Attributes Inspector different from the system font - don't change anything - neither simulator or storyboard. I even tried to set the font programatically with Attributed string - the same System font appears. Thanks for your help.
You cannot use the included iOS fonts in WatchKit at this time. The only one available is System (San Francisco). Source: Apple Developer Forums
You can however use a custom font by adding the font file to the project:
Drag the font files into the project navigator
Include the custom font file in both your WatchKit app and WatchKit extension bundle.
Add the Fonts provided by application (UIAppFonts) key to both your WatchKit app and your WatchKit Extension Info.plist files
Add this code to
awakeWithContext
to make sure you know the correct font name to call later in your code:print("Custom font names:") print(UIFont.fontNames(forFamilyName: "Exo")) print(UIFont.fontNames(forFamilyName: "Tabardo"))
Run the app and take note of the font names printed to the debug console. Once you know the correct name, you can add this code somewhere in your WatchKit Extension:
var fontSize = CGFloat(32) var text = "so cool" var cstmFont = UIFont(name: "Tabardo", size: fontSize)! var attrStr = NSAttributedString(string: text, attributes: [NSFontAttributeName: cstmFont]) firstLabel.setAttributedText(attrStr) fontSize = CGFloat(36) text = "right on!" cstmFont = UIFont(name: "Exo-Regular", size: fontSize)! attrStr = NSAttributedString(string: text, attributes: [NSFontAttributeName: cstmFont]) secondLabel.setAttributedText(attrStr)
Enjoy the custom fonts on the watch!
Keep in mind that glances and notifications cannot use custom fonts. If you want to use one there, you'll have to use a rendered image. However because glances and notifications should load quickly you'd want to have that image ready to go when it gets called.
这篇关于WatchKit WKInterfaceLabel不能更改字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!