gpt4 book ai didi

ios - Swift 4 iOS - 在不影响 Storyboard字体大小的情况下完全覆盖系统字体

转载 作者:可可西里 更新时间:2023-11-01 01:07:33 28 4
gpt4 key购买 nike

我愿意:

  1. 覆盖所有应用程序中的默认字体,从 Storyboard中选择字体大小
  2. 如果可能,请在 Storyboard 中可视化的文本中查看自定义字体的预览。

我成功地插入了自定义字体,但我在调整文本大小方面遇到了问题。

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

  1. AppDelegate 中添加了这一行在 didFinishLaunchingWithOptions 函数中:
    UILabel.appearance().font = UIFont(name: "yourFont", size: yourSize)
    这会覆盖所有 UILabel 的字体,但也会覆盖字体大小。所有标签都带有相同的 yourSize字体大小。
  2. 我扩展了 UILabel,强制它执行 changeFontName。

    extension UILabel {
    override open func awakeFromNib() {
    super.awakeFromNib()
    changeFontName()
    }

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

    这是可行的,但 Storyboard显然不会更新 View 。而且我不确定这样做是否正确

最佳答案

我采用的解决方案类似于#2 解决方案,但它也在 Storyboard上呈现字体。

我们创建了一些扩展默认 UIView 的类,例如 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)
}

}

然后在 Storyboard中,我们在 Identity Inspector 中为每个必须更改字体的组件设置该类作为 Custom Class

这不是最干净的解决方案,但至少如果您在整个应用程序中更改 MyCustomFont 字体,您只需通过代码一次性更改它。

关于ios - Swift 4 iOS - 在不影响 Storyboard字体大小的情况下完全覆盖系统字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54494170/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com