ios - 如何在 Interface Builder 中使用带有属性文本的 SF 字体

标签 ios xcode fonts interface-builder nsattributedstring

我在 Xcode 9.x 上,我想在 UILabel 上使用属性文本(在 InterfaceBuilder 中)。

我想设置:系统字体(San Fracisco)和一些粗体风格的文字。但它不起作用(斜体 起作用但粗体 ...)。

除非我在 Xcode 项目中导入所有 San Francisco 字体,否则我无法选择系统字体 (San Francisco Pro/Display)(但它非常重:15Mb!)

请问你是怎么做到的?谢谢。

最佳答案

我能够通过直接编辑 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>

关于ios - 如何在 Interface Builder 中使用带有属性文本的 SF 字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47594889/

相关文章:

iOS 兼容的实时 mp4 流

iphone - SQLCipher "sqlite_3key"归档和分析警告,调试无警告

python - SwiftUI - 用 Python 和模块库打包

windows-phone-7 - WP7 - 不拾取嵌入字体

objective-c - 如何确定 NSFont 是否安装在 Objective-C 的 Mac OSX 机器上?

ios - 如何使用 NSUserDefaults 将项目添加到 NSMutableArray

ios - 如何使用 Swift 更改 iOS 中 UIButton 的状态?

iphone - 是否可以在Snow Leopard上获得Xcode 4.2的iOS 6.1 SDK?

ios - 带有自动布局的 Storyboard UITableViewCell

css - 粗体文本跳回正常