斯威夫特4的iOS - 完全覆盖系统字体不影响故事板字体大小

问题描述 投票:2回答:1

我想要:

  1. 覆盖默认的字体在所有的应用程序从情节提要选择字体大小
  2. 如果不可能性看到自定义字体的预览在故事板可视化的文本。

我设法插入自定义字体,但我在文本大小的问题。

我尝试了以下解决方案,但他们仍然不完全符合我的要求:

  1. 新增的didFinishLaunchingWithOptions FUNC内AppDelegate该行: UILabel.appearance().font = UIFont(name: "yourFont", size: yourSize) 这将覆盖字体为所有UILabels但它覆盖的字体大小了。所有标签带有相同yourSize字体大小。
  2. 我延长了的UILabel迫使其执行changeFontName。 extension UILabel { override open func awakeFromNib() { super.awakeFromNib() changeFontName() } func changeFontName() { self.font = UIFont(name: "yourFont", size: self.font.pointSize) } } 这是工作,但情节串连图板显然不更新视图。而且我不知道它是做它的正确方法
ios swift xcode fonts swift4.2
1个回答
2
投票

该解决方案我走近它类似于#2 solution,但它呈现的故事板的字体了。

我们创造了一些类,扩展默认UIViews,例如用于UILabelUIButton。你可以这样做:

@IBDesignable
public class CustomUILabel: UILabel {

    public override func awakeFromNib() {
        super.awakeFromNib()
        configureLabel()
    }

    public override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        configureLabel()
    }

    func configureLabel() {
        font = UIFont(name: "MyCustomFont", size: self.font.pointSize)
    }

}

@IBDesignable
public class CustomUIButton: UIButton {

    public override func awakeFromNib() {
        super.awakeFromNib()
        configureLabel()
    }

    public override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        configureLabel()
    }

    func configureLabel() {
        titleLabel?.font = UIFont(name: "MyCustomFont", size: self.titleLabel!.font.pointSize)
    }

}

然后在故事板,我们在Identity Inspector该类作为Custom Class设置为不得不改变字体的每个组件。

这是不是最干净的解决方案,但至少如果你改变了MyCustomFont字体在整个应用程序,您只可以在一个单次改变它的代码。

© www.soinside.com 2019 - 2024. All rights reserved.