如何在WKInterfaceLabel中使用替代字体粗细(例如"short")? [英] How to use alternate font weights (e.g. "short") in a WKInterfaceLabel?
问题描述
在Apple的 Apple Watch设计资源中,有一个名为 Alternate Font Weights和斜体.在该示例中,显示了默认字体的示例,这些默认字体具有不同的属性,例如,对其应用了 Italic , Emphasized 和 Short ./p>
假设我想使用一种看起来与"Short"示例完全相同的字体.
-
是否可以通过情节提要为标签选择此字体和样式?
-
在代码中,如何设置标签以使用此字体和样式?
在iOS项目中,我尝试修改正文字体(UIFontTextStyleBody
)使其包含UIFontDescriptorTraitTightLeading
.当我读取fontAttributes
属性时,它会将NSFontNameAttribute
报告为.AppleSystemUIShortBody
(原始值为.AppleSystemUIBody
).
由此,我能够弄清楚苹果的条款:
+------------+-----------------------------------+------------------------------+---------------------------------+
| Term | UIFontDescriptorSymbolicTraits | NSFontNameAttribute | NSCTFontUIUsageAttribute |
+------------+-----------------------------------+------------------------------+---------------------------------+
| | (none) | .AppleSystemUIBody | UICTFontTextStyleBody |
| Italic | UIFontDescriptorTraitItalic | .AppleSystemUIItalicBody | UICTFontTextStyleItalicBody |
| Emphasized | UIFontDescriptorTraitBold | .AppleSystemUIEmphasizedBody | UICTFontTextStyleEmphasizedBody |
| Short | UIFontDescriptorTraitTightLeading | .AppleSystemUIShortBody | UICTFontTextStyleShortBody |
| Tall? | UIFontDescriptorTraitLooseLeading | .AppleSystemUISpliceTallBody | UICTFontTextStyleTallBody |
+------------+-----------------------------------+------------------------------+---------------------------------+
如果尝试使用UIFontDescriptorTraitExpanded
,UIFontDescriptorTraitCondensed
,UIFontDescriptorTraitVertical
,UIFontDescriptorTraitUIOptimized
做相同的事情,然后读取fontAttributes
,它将返回与主体"样式相同的值.如果您使用UIFontDescriptorTraitMonoSpace
尝试相同的操作,它将返回nil
字典.
因此,在Apple Watch项目中,您应该能够使用 Body Short Empharated 字体,如下所示:
/*1*/ UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
/*2*/ fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitTightLeading | UIFontDescriptorTraitBold];
/*3*/ UIFont *font = [UIFont fontWithDescriptor:fontDescriptor size:0];
/*4*/ [label setAttributedText:[[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: font}]];
我说应该"是因为这里有两个错误:
- 在第2行上,指定了
UIFontDescriptorTraitTightLeading
,但是在两个手表模拟器上都忽略了此特征.粗体和斜体效果很好. - 在第3行上,由于
size
设置为0
,因此它从fontDescriptor
未自定义字体大小时,两种方法均返回16
,显然应该选择15
在38毫米设备上. (您可以通过在故事板上放置标签,为其指定 Body 大小,然后将其与将其大小手动设置为15的标签进行视觉比较来进行验证.)
注意:我正在运行Xcode 6.2.
In Apple's Apple Watch Design Resources, there is a document called Alternate Font Weights and Italicizations. In it, it shows examples of what the default font looks like with different attributes such as Italic, Emphasized, and Short applied to it.
Assume I want to use a font that looks exactly like the "Short" example.
Is there any way to choose this font and style via the storyboard for a label?
In code, how would I set a label to use this font and style?
In an iOS project, I tried modifying the body font (UIFontTextStyleBody
) to include UIFontDescriptorTraitTightLeading
. When I read the fontAttributes
properties, it reports the NSFontNameAttribute
as .AppleSystemUIShortBody
(the original value is .AppleSystemUIBody
).
From this, I was able to figure out Apple's terms:
+------------+-----------------------------------+------------------------------+---------------------------------+
| Term | UIFontDescriptorSymbolicTraits | NSFontNameAttribute | NSCTFontUIUsageAttribute |
+------------+-----------------------------------+------------------------------+---------------------------------+
| | (none) | .AppleSystemUIBody | UICTFontTextStyleBody |
| Italic | UIFontDescriptorTraitItalic | .AppleSystemUIItalicBody | UICTFontTextStyleItalicBody |
| Emphasized | UIFontDescriptorTraitBold | .AppleSystemUIEmphasizedBody | UICTFontTextStyleEmphasizedBody |
| Short | UIFontDescriptorTraitTightLeading | .AppleSystemUIShortBody | UICTFontTextStyleShortBody |
| Tall? | UIFontDescriptorTraitLooseLeading | .AppleSystemUISpliceTallBody | UICTFontTextStyleTallBody |
+------------+-----------------------------------+------------------------------+---------------------------------+
If you attempt to do the same thing with UIFontDescriptorTraitExpanded
, UIFontDescriptorTraitCondensed
, UIFontDescriptorTraitVertical
, UIFontDescriptorTraitUIOptimized
, and then read the fontAttributes
, it returns the same value as the Body style. If you try the same thing with UIFontDescriptorTraitMonoSpace
, it returns a nil
dictionary.
Therefore, in an Apple Watch project, you should be able to use the Body Short Emphasized font like so:
/*1*/ UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
/*2*/ fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitTightLeading | UIFontDescriptorTraitBold];
/*3*/ UIFont *font = [UIFont fontWithDescriptor:fontDescriptor size:0];
/*4*/ [label setAttributedText:[[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: font}]];
I say "should" because there are two bugs in play here:
- On line 2,
UIFontDescriptorTraitTightLeading
is being specified, yet this trait is ignored on both watch simulators. Bold and italic work just fine. - On line 3, since
size
is set to0
, it gets its value from theUIFontDescriptorSizeAttribute
infontDescriptor
[1]. The problem is that on a 38mm device, there is a bug with both+[UIFontDescriptor preferredFontDescriptorWithTextStyle:]
and-[UIFont preferredFontForTextStyle:]
(used on line 1). When the user doesn't customize the font size, both methods return16
, when clearly15
should be chosen on the 38mm device. (You can verify this by placing a label on a storyboard, assigning it the Body size, and visually comparing it to a label that sets its size to 15 manually.)
Note: I am running Xcode 6.2.
这篇关于如何在WKInterfaceLabel中使用替代字体粗细(例如"short")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!