如何获得尊重可访问性设置的等宽字体

时间:2017-10-09 08:51:37

标签: ios swift fonts ios9 accessibility

let bodyFontDescriptor = UIFontDescriptor
    .preferredFontDescriptor(withTextStyle: UIFontTextStyle.body)
let bodyMonospacedFontDescriptor = bodyFontDescriptor.addingAttributes(
    [
        UIFontDescriptorFeatureSettingsAttribute: [
            [
                UIFontFeatureTypeIdentifierKey: kTextSpacingType,
                UIFontFeatureSelectorIdentifierKey: kMonospacedTextSelector
            ]
        ]
    ])
let bodyMonospacedFont = UIFont(descriptor: bodyMonospacedFontDescriptor, size: 0.0)
textview.font = bodyMonospacedFont

这将生成带有可变宽度字符的文本。 我需要获得一个monospace字体而不需要硬编码快递 和固定的大小。 部署目标是ios 9.0

1 个答案:

答案 0 :(得分:3)

这里是UIFontDescriptor的扩展,它为给定的文本样式返回首选的等宽字体描述符。没有简单的方法使用UIFontUIFontDescriptor来获得完全等宽的字体。此解决方案试图找到一种好的等宽字体,并在需要时退回给Courier。

extension UIFontDescriptor {
    static let monoDescriptor: UIFontDescriptor = {
        // Attempt to find a good monospaced, non-bold, non-italic font
        for family in UIFont.familyNames {
            for name in UIFont.fontNames(forFamilyName: family) {
                let f = UIFont(name: name, size: 12)!
                let fd = f.fontDescriptor
                let st = fd.symbolicTraits
                if st.contains(.traitMonoSpace) && !st.contains(.traitBold) && !st.contains(.traitItalic) && !st.contains(.traitExpanded) && !st.contains(.traitCondensed) {
                    return fd
                }
            }
        }

        return UIFontDescriptor(name: "Courier", size: 0) // fallback
    }()

    class func preferredMonoFontDescriptor(withTextStyle style: UIFontTextStyle) -> UIFontDescriptor {
        // Use the following line if you need a fully monospaced font
        let monoDescriptor = UIFontDescriptor.monoDescriptor

        // Use the following two lines if you only need monospaced digits in the font
        //let monoDigitFont = UIFont.monospacedDigitSystemFont(ofSize: 0, weight: .regular)
        //let monoDescriptor = monoDigitFont.fontDescriptor

        // Get the non-monospaced preferred font
        let defaultFontDescriptor = preferredFontDescriptor(withTextStyle: style)
        // Remove any attributes that specify a font family or name and remove the usage
        // This will leave other attributes such as size and weight, etc.
        var fontAttrs = defaultFontDescriptor.fontAttributes
        fontAttrs.removeValue(forKey: .family)
        fontAttrs.removeValue(forKey: .name)
        fontAttrs.removeValue(forKey: .init(rawValue: "NSCTFontUIUsageAttribute"))
        let monospacedFontDescriptor = monoDescriptor.addingAttributes(fontAttrs)

        return monospacedFontDescriptor.withSymbolicTraits(defaultFontDescriptor.symbolicTraits) ?? monospacedFontDescriptor
    }
}

请注意有关您是需要完全等宽字体还是仅具有等宽数字位数字体的注释。注释/取消注释这些行以适合您的特定需求。

样品用量:

let bodyMonospacedFont = UIFont(descriptor: .preferredMonoFontDescriptor(withTextStyle: .body), size: 0)
textview.font = bodyMonospacedFont

以下是一些测试代码,用于确认preferredMonoFontDescriptor(withTextStyle:)的结果适用于所有样式:

let textStyles: [UIFontTextStyle] = [ .body, .callout, .caption1, .caption2, .footnote, .headline, .subheadline, .largeTitle, .title1, .title2, .title3 ]
for style in textStyles {
    let nfont = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: style), size: 0)
    let mfont = UIFont(descriptor: .preferredMonoFontDescriptor(withTextStyle: style), size: 0)
    print(style)
    print(nfont)
    print(mfont)
}

如果比较每对结果,它们的大小,粗细和样式相同,只是字体不同。