【问题标题】:Change all UILabels for a specific UIFont.TextStyle更改特定 UIFont.TextStyle 的所有 UILabel
【发布时间】:2019-07-25 23:14:26
【问题描述】:

我在我的应用程序中使用TextStyles 来支持动态字体。我面临为每个TextStyle 更改字体的挑战。例如,TextStyle.body 应该是 MyAwesomeBODYFontTextStyle.headline 应该是 MyAwesomeHeadlineFont。这适用于整个应用程序。为整个应用设置字体将不起作用,因为我需要为不同样式设置多个字体。

是否可以以某种方式为整个应用程序而不是为每个标签单独使用自定义字体覆盖这些TextStyles

我尝试了什么:

UILabelappearance 代理设置字体通常可以正常工作:

let labelAppearance = UILabel.appearance()
let fontMetrics = UIFontMetrics(forTextStyle: .body)
labelAppearance.font = fontMetrics.scaledFont(for: myAwesomeBodyFont)

但这会覆盖所有标签,无论他们使用什么TextStyle

之后我尝试检查 TextStyle,但它因 UILabel.appearance().font 的 nil 指针异常而崩溃,或者甚至没有进入 if 块。

let labelAppearance = UILabel.appearance()
if let textStyle = labelAppearance.font.fontDescriptor.object(forKey: UIFontDescriptor.AttributeName.textStyle) as? UIFont.TextStyle {
    // this would be the place to check for the TextStyle and use the corresponding font

    let fontMetrics = UIFontMetrics(forTextStyle: textStyle)
    labelAppearance.font = fontMetrics.scaledFont(for: mayAwesomeBodyFont)
}

因为 UILabel 的外观没有 font 集。

【问题讨论】:

  • 您不能直接“设置”文本样式的自定义字体。我建议搜索dynamic type custom font - 你会找到很多资源。这是一个开始的地方:useyourloaf.com/blog/using-a-custom-font-with-dynamic-type
  • 感谢搜索推荐!我实际上已经搜索了确切的术语并使用了这篇文章 :) 我稍微指定了我的问题。
  • TypographyKit 是一个可以帮助解决这个问题的第三方模块。
  • TypographyKit 在我发现它时听起来很有趣,但它也为每个标签设置了这个。用户界面组件。无论如何感谢您的评论,我再次查看了框架,但找不到针对该特定问题的解决方案。

标签: ios swift cocoa-touch


【解决方案1】:

您不能直接“设置”文本样式的自定义字体。 您可以获取文本样式的字体大小,然后可以使用自定义系列。

let systemDynamicFontDescriptor = UIFontDescriptor.preferredFontDescriptorWithTextStyle(UIFontTextStyleBody)
let size = systemDynamicFontDescriptor.pointSize
let font = UIFont(name: MyAwesomeBODYFont, size: size)

iOS 11+ 有scaledFont()

您可以将此字体变量设为静态,并可以在应用程序的任何地方使用它。

你也可以看看这个解决方案:https://stackoverflow.com/a/42235227/4846167

【讨论】:

  • 感谢@Aaiana 的回答。也许我的问题不是很具体。我编辑我的帖子。我正在寻找一个通用的解决方案,我不需要为每个标签都设置这个。
  • 明白你的意思。也许看看这个curtclifton.net/fonts-with-style。文章很旧,但可以提供一些想法。
【解决方案2】:

我最终创建了一个 UILabel 的子类,并让我的所有标签都继承自它。这样您就可以在 InterfaceBuilder 中设置类和/或在代码中创建自定义类。

这是 DynamicCustomFontLabel 类:

import UIKit

class DynamicCustomFontLabel: UILabel {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!

        initCustomFont()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        initCustomFont()
    }

    private func initCustomFont() {
        if let textStyle = font.fontDescriptor.object(forKey: UIFontDescriptor.AttributeName.textStyle) as? UIFont.TextStyle {
            let fontMetrics = UIFontMetrics(forTextStyle: textStyle)
            var customFont: UIFont?

            switch textStyle {
            case .body:
                customFont = UIFont(name: "MyAwesomeBODYFont", size: 21)

            case .headline:
                customFont = UIFont(name: "MyAwesomeHeadlineFont", size: 48)

            // all other cases...

            default:
                return
            }

            guard let font = customFont else {
                fatalError("Failed to load a custom font! Make sure the font file is included in the project and the font is added to the Info.plist.")
            }

            self.font = fontMetrics.scaledFont(for: font)
        }
    }
}

【讨论】:

  • 你能提供使用这个类的例子吗?
  • @marika.daboja 您可以将它用作您将使用的任何其他 UILabel,因为该类继承自它。
猜你喜欢
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多