如何在 Interface Builder 中使用带有属性文本的 SF 字体
How to use SF Fonts with attributed text in Interface Builder
我在 Xcode 9.x 并且我想在 UILabel 上使用属性文本(在 InterfaceBuilder 中)。
我想设置:系统字体 (San Fracisco) 和 粗体 风格的一些文字。但它不起作用(italic 有效但 bold ...)。
我无法选择系统字体(San Francisco Pro/Display),除非我在 Xcode 项目中导入所有 San Francisco 字体(但它非常重:15Mb!)
请问你是怎么做到的?谢谢
据我所知,您不能严格在 Interface Builder 中执行此操作。
但是,如果您对封闭视图或标签视图本身进行子类化,则可以轻松地在 awakeFromNib() 中重置字体,同时仍然保留 Interface Builder 中标签的其余设置。这将节省您完全从头开始以编程方式创建它的时间。
像往常一样在IB中设置标签,包括属性、约束、动作。使用任意字体进行预览。确保按住 Ctrl 键拖动样式标签的出口,然后使用如下代码在 awakeFromNib 中切换字体。当您的应用程序运行时,它将使用系统字体,以及您已经在 IB 中建立的所有属性、位置限制等。
[编辑:已更正以添加粗体字重,不会从 IB 继承。]
例如:
class EnclosingViewSubclass : UIView {
// The outlet you create for the label
@IBOutlet weak var styledLabel: UILabel!
// ...
override func awakeFromNib() {
super.awakeFromNib()
// setting the label's font face to the system font, including picking
// up the font-size you establish in InterfaceBuilder
styledLabel.font = UIFont.systemFont(ofSize: styledLabel.font.pointSize,
weight: UIFontWeightBold)
}
}
我能够通过直接编辑 XML 完全在 Interface Builder(有点)中破解它。根据需要设置属性字符串,然后找到 <font />
并根据需要进行编辑。例如,我的按钮使用系统粗体。现在,字体显示在 IB 中,既出现在属性字符串的视觉呈现中,也出现在属性中。我可以更改大小并保持粗体。
<font key="NSFont" size="16" name=".AppleSystemUIFontBold"/>
这是在上下文中:
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="wordWrap" hasAttributedTitle="YES" translatesAutoresizingMaskIntoConstraints="NO" id="1mt-rM-2b8">
<rect key="frame" x="211" y="8" width="187" height="52"/>
<color key="backgroundColor" red="0.0" green="0.41960784309999999" blue="0.72156862749999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<inset key="contentEdgeInsets" minX="4" minY="16" maxX="4" maxY="16"/>
<state key="normal">
<attributedString key="attributedTitle">
<fragment content="Okay, I'll fill it out now">
<attributes>
<color key="NSColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<font key="NSFont" size="16" name=".AppleSystemUIFontBold"/>
</attributes>
</fragment>
</attributedString>
</state>
<connections>
...
</connections>
</button>
我在 Xcode 9.x 并且我想在 UILabel 上使用属性文本(在 InterfaceBuilder 中)。
我想设置:系统字体 (San Fracisco) 和 粗体 风格的一些文字。但它不起作用(italic 有效但 bold ...)。
我无法选择系统字体(San Francisco Pro/Display),除非我在 Xcode 项目中导入所有 San Francisco 字体(但它非常重:15Mb!)
请问你是怎么做到的?谢谢
据我所知,您不能严格在 Interface Builder 中执行此操作。
但是,如果您对封闭视图或标签视图本身进行子类化,则可以轻松地在 awakeFromNib() 中重置字体,同时仍然保留 Interface Builder 中标签的其余设置。这将节省您完全从头开始以编程方式创建它的时间。
像往常一样在IB中设置标签,包括属性、约束、动作。使用任意字体进行预览。确保按住 Ctrl 键拖动样式标签的出口,然后使用如下代码在 awakeFromNib 中切换字体。当您的应用程序运行时,它将使用系统字体,以及您已经在 IB 中建立的所有属性、位置限制等。
[编辑:已更正以添加粗体字重,不会从 IB 继承。]
例如:
class EnclosingViewSubclass : UIView {
// The outlet you create for the label
@IBOutlet weak var styledLabel: UILabel!
// ...
override func awakeFromNib() {
super.awakeFromNib()
// setting the label's font face to the system font, including picking
// up the font-size you establish in InterfaceBuilder
styledLabel.font = UIFont.systemFont(ofSize: styledLabel.font.pointSize,
weight: UIFontWeightBold)
}
}
我能够通过直接编辑 XML 完全在 Interface Builder(有点)中破解它。根据需要设置属性字符串,然后找到 <font />
并根据需要进行编辑。例如,我的按钮使用系统粗体。现在,字体显示在 IB 中,既出现在属性字符串的视觉呈现中,也出现在属性中。我可以更改大小并保持粗体。
<font key="NSFont" size="16" name=".AppleSystemUIFontBold"/>
这是在上下文中:
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="wordWrap" hasAttributedTitle="YES" translatesAutoresizingMaskIntoConstraints="NO" id="1mt-rM-2b8">
<rect key="frame" x="211" y="8" width="187" height="52"/>
<color key="backgroundColor" red="0.0" green="0.41960784309999999" blue="0.72156862749999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<inset key="contentEdgeInsets" minX="4" minY="16" maxX="4" maxY="16"/>
<state key="normal">
<attributedString key="attributedTitle">
<fragment content="Okay, I'll fill it out now">
<attributes>
<color key="NSColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<font key="NSFont" size="16" name=".AppleSystemUIFontBold"/>
</attributes>
</fragment>
</attributedString>
</state>
<connections>
...
</connections>
</button>