代码之家  ›  专栏  ›  技术社区  ›  Jarvis The Avenger adri

在整个应用程序中动态更改字体系列

  •  0
  • Jarvis The Avenger adri  · 技术社区  · 7 年前

    我在一个多目标项目,其中包括两个应用程序,每个都有自己的品牌。

    我试图找到一种方法来保持不同字体系列的字体大小相同,但没有成功,请让我知道,如果你有任何建议来解决这个特殊的情况。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Baig    7 年前

    使用在几分钟内更新整个应用程序的视觉外观 NUI Theme1.nss Theme2.nss 在两个不同的文件中 application didFinishLaunchingWithOptions 方法只需告诉NUI框架将这些主题相应地加载到目标

    如果目标1

    [NUISettings initWithStylesheet:@"Theme1"];    
    

    #elif目标2

    [NUISettings initWithStylesheet:@"Theme2"];
    

    #结束

        2
  •  0
  •   Jarvis The Avenger adri    7 年前

    我找到了一个方法来解决这个问题通过方法swizzling。以下是解决此问题的步骤:

    • 步骤1:创建UIFont的扩展名和以下代码以覆盖字体名称和族运行时。

    代码示例:

      var appFontName = "ArialMT"
      var appFontBoldName = "Arial-BoldMT"
    
     extension UIFontDescriptor.AttributeName {
       static let nsctFontUIUsage = UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute")
     } 
    
    extension UIFont {
    
    @objc class func mySystemFont(ofSize size: CGFloat) -> UIFont {
        return UIFont(name: appFontName, size: size)!
    }
    
    @objc class func myBoldSystemFont(ofSize size: CGFloat) -> UIFont {
        return UIFont(name: appFontBoldName, size: size)!
    }
    
    @objc convenience init(myCoder aDecoder: NSCoder) {
    
        guard let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as? UIFontDescriptor else  {
            self.init(myCoder: aDecoder)
            return
        }
        let face = fontDescriptor.object(forKey: UIFontDescriptor.AttributeName.face) as? String ?? "Regular"
        var fontName = appFontName
    
        switch face {
        case "Regular":
            fontName = appFontName
        case "Bold":
            fontName = appFontBoldName
        default:
            fontName = appFontName
        }
        self.init(name: fontName, size: fontDescriptor.pointSize)!
    }
    
    class func overrideInitialize() {
        if self == UIFont.self {
            let systemFontMethod = class_getClassMethod(self, #selector(systemFont(ofSize:)))
            let mySystemFontMethod = class_getClassMethod(self, #selector(mySystemFont(ofSize:)))
            method_exchangeImplementations(systemFontMethod!, mySystemFontMethod!)
    
            let boldSystemFontMethod = class_getClassMethod(self, #selector(boldSystemFont(ofSize:)))
            let myBoldSystemFontMethod = class_getClassMethod(self, #selector(myBoldSystemFont(ofSize:)))
            method_exchangeImplementations(boldSystemFontMethod!, myBoldSystemFontMethod!)
    
            let initCoderMethod = class_getInstanceMethod(self, #selector(UIFontDescriptor.init(coder:)))
            let myInitCoderMethod = class_getInstanceMethod(self, #selector(UIFont.init(myCoder:)))
            method_exchangeImplementations(initCoderMethod!, myInitCoderMethod!)
        }
      }
    }
    
    • 步骤2:(用法)并调用overrideInitialize方法。

      UIFont.overrideInitialize()
      

    它通过在整个应用程序中更改字体系列来保留字体大小。:)