【问题标题】:Replace font for every UILabel in the App替换 App 中每个 UILabel 的字体
【发布时间】:2019-06-30 10:43:47
【问题描述】:

我正在尝试通过为 UILabel 制作扩展名来更改字体

extension UILabel{
    var defaultFont: UIFont? {
        get { return self.font }
        set {
            let oldFontSize = self.font.pointSize
            let oldFontName = self.font.fontName
            let newFontName  = newValue?.fontName
            self.font = UIFont(name: newFontName!, size: oldFontSize)  
        }
    }
}

然后调用

UILabel.appearance().defaultFont = UIFont.init(name: "My Font", size: 5)

self.font 总是nil 使用 xcode 10 swift 4.2

编辑: 现在,将 swift 版本更改为 Swift3 后它运行良好,那么问题出在 Swift4 上。

是否有解决方案或替代方法?

【问题讨论】:

    标签: ios swift uifont swift4.2


    【解决方案1】:

    试试这个: 是 appDelegate -> didFinishLaunch:

    UILabel.appearance().font = UIFont("your font name", size: 15)
    

    【讨论】:

    • 那么你应该创建两个扩展 UiLabel 的类。并使用它们来设置字体和大小自定义
    【解决方案2】:

    标准的做法是创建您的自定义UILabel 类,并在您需要默认字体的任何地方使用该类。例如,

    class MyFontLabel: UILabel {
    
        override init(frame: CGRect) {
            super.init(frame: frame)
    
            self.commonInit()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
    
            self.commonInit()
        }
    
        private func commonInit() {
            self.font = UIFont(name: "My Font", size: self.font.pointSize)
        }
    }
    

    【讨论】:

      【解决方案3】:

      因此,虽然您在技术上 can't override an instance variable,也就是覆盖 UILabel 的字体变量不起作用,但您可以扩展 UILabel。

      public class CustomLabel: UILabel {
          override public var font: UIFont! {
              get { return super.font }
              set {
                  super.font = UIFont(name: "Font-Name", size: super.font.pointSize)
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        我发现在 UILabel 的扩展中添加 style(as style: TypographyStyle) 函数最有帮助,其中 TypographyStyle 是我定义的包含一堆样式属性(包括字体)的枚举。我还为 UILabel 创建了一个方便的 init,它采用 TypographyStyle 的大小写,并调用 style(as:),以便在创建标签时自动应用样式。

        extension UILabel {
            convenience init(style: TypographyStyle) {
                self.init()
        
                self.style(as: style)
            }
        
            func style(as style: TypographyStyle) {
                self.font = UIFont(name: style.fontName, size: style.fontSize)
            }
        }
        
        enum TypographyStyle {
            case body, h1, h2, h3
        
            var font: UIFont {
                switch self {
                // here you can return different fonts for the different styles if you like
                default:
                    return UIFont(name: "AvenirNext", size: 16)
                }
            }
        }
        
        // Usage
        let label = UILabel(style: .body)
        

        【讨论】:

          【解决方案5】:

          把这个放在appDelegatedidFinishLaunchingWithOptions方法中:

          UILabel.appearance().font = UIFont(name: "your font name", size: UILabel.appearance().font.pointSize)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-07-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-31
            • 1970-01-01
            • 1970-01-01
            • 2018-06-04
            相关资源
            最近更新 更多