Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 4 iOS-完全覆盖系统字体,而不影响故事板字体大小_Ios_Swift_Xcode_Fonts_Swift4.2 - Fatal编程技术网

Swift 4 iOS-完全覆盖系统字体,而不影响故事板字体大小

Swift 4 iOS-完全覆盖系统字体,而不影响故事板字体大小,ios,swift,xcode,fonts,swift4.2,Ios,Swift,Xcode,Fonts,Swift4.2,我想: 覆盖所有应用程序中的默认字体,从情节提要中选择字体大小 如果可能,请在脚本中显示的文本中查看自定义字体的预览。 我设法插入了自定义字体,但文本大小有问题 我尝试了以下解决方案,但仍然不完全符合我的要求: 在didFinishLaunchingWithOptions func:UILabel.appearance()中的AppDelegate中添加了此行。font=UIFont(名称:“yourFont”,大小:yourSize) 这会覆盖所有UILabel的字体,但也会覆盖字体大小。 所

我想:

  • 覆盖所有应用程序中的默认字体,从情节提要中选择字体大小
  • 如果可能,请在脚本中显示的文本中查看自定义字体的预览。
  • 我设法插入了自定义字体,但文本大小有问题

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

  • 在didFinishLaunchingWithOptions func:
    UILabel.appearance()中的
    AppDelegate
    中添加了此行。font=UIFont(名称:“yourFont”,大小:yourSize)

    这会覆盖所有UILabel的字体,但也会覆盖字体大小。 所有标签都具有相同的
    yourSize
    font size
  • 我扩展了UILabel,强制它执行changeFontName。

    extension UILabel {
        override open func awakeFromNib() {
            super.awakeFromNib()
            changeFontName()
        }
    
        func changeFontName() {
            self.font = UIFont(name: "yourFont", size: self.font.pointSize)
        }
    }
    
    这是可行的,但情节提要显然不会更新视图。我不确定这样做是否正确


  • 我采用的解决方案类似于
    #2解决方案
    ,但它也会在故事板上呈现字体

    我们创建了一些扩展默认UIView的类,例如
    UILabel
    UIButton
    。您可以这样做:

    @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类
    ,用于每个必须更改字体的组件

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