QLa*_*Lag 8 xcode fonts interface-builder nsattributedstring ios
我在Xcode 9.x上,我想在UILabel上使用属性文本(在InterfaceBuilder中).
我想设置:系统字体(San Fracisco)和一些粗体风格的单词.但它不起作用(斜体有效,但不大胆 ......).
我不能选择系统字体(旧金山专业版/显示版),除非我在Xcode项目中导入所有旧金山字体(但它很重:15Mb!)
你觉得怎么样?谢谢.
Daw*_*oth 11
通过直接编辑XML,我完全可以在Interface Builder(某种程度上)中破解它。根据需要设置属性字符串,然后根据需要查找<font />和编辑它。例如,我的按钮使用系统粗体。现在,字体在IB中显示,既显示在属性字符串中,也显示在属性中。我可以更改大小并保持粗体。
<font key="NSFont" size="16" name=".AppleSystemUIFontBold"/>
Run Code Online (Sandbox Code Playgroud)
在上下文中:
<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>
Run Code Online (Sandbox Code Playgroud)
据我所知,您不能在 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)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1779 次 |
| 最近记录: |