gpt4 book ai didi

ios - 如何以正确的方式在整个 IOS 应用程序中使用自定义字体

转载 作者:行者123 更新时间:2023-12-01 18:03:32 24 4
gpt4 key购买 nike

我对这个话题做了很多调查,但是我还没有找到的方法。轻松为整个 IOS 应用设置自定义字体 .我需要为一个非常大的应用程序更改整个字体,而且我已经在 Android 中非常容易地做到了。
解决方案的答案似乎是 过时的 objective-C , 或 太复杂了对于这个任务。
到目前为止我找到了 3 主 方法:

  • Traditional approach:使用枚举或扩展。问题是如果应用程序已经高级,将字体应用于每个 View 真的很乏味。
  • Using extensions of views and .appearance() .问题在于它将字体设置为每个 View ,您可能希望一个 UILabel 使用一种字体,另一个 UILabel 使用另一种字体。
  • Using a "Hack" (似乎是一个很好的解决方案)问题是它有风险并且没有 future 的证明,因为将来苹果可能会在应用商店中拒绝这样的应用。这种方法也改变了一些系统 UI 字体,因此它更像是一种 hack,而不是原生解决方案。

  • 这个解决方案是我找到的,但它们并不完全灵活。 Android has a great way to do it (只需在xml和tadaa中添加一个字体系列,系统会自动选择正确的),我相信IOS可能会有。
    还有其他方法吗?否则, 推荐的方法是什么 .

    最佳答案

    基于 this site并进行一些调整

    第一:你必须在你的项目中添加你的字体,并将它们添加到 info.plist文件:
    https://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

    文件:UIFont.swift

    import Foundation
    import UIKit

    struct AppFontName {
    static let regular = "Montserrat-Regular"
    static let bold = "Montserrat-Bold"
    static let lightAlt = "Montserrat-Light"
    }
    //customise font
    extension UIFontDescriptor.AttributeName {
    static let nsctFontUIUsage = UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute")
    }

    extension UIFont {

    @objc class func mySystemFont(ofSize size: CGFloat) -> UIFont {
    return UIFont(name: AppFontName.regular, size: size)!
    }

    @objc class func myBoldSystemFont(ofSize size: CGFloat) -> UIFont {
    return UIFont(name: AppFontName.bold, size: size)!
    }

    @objc class func myItalicSystemFont(ofSize size: CGFloat) -> UIFont {
    return UIFont(name: AppFontName.lightAlt, size: size)!
    }

    @objc convenience init(myCoder aDecoder: NSCoder) {
    guard
    let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as? UIFontDescriptor,
    let fontAttribute = fontDescriptor.fontAttributes[.nsctFontUIUsage] as? String else {
    self.init(myCoder: aDecoder)
    return
    }
    var fontName = ""
    switch fontAttribute {
    case "CTFontRegularUsage":
    fontName = AppFontName.regular
    case "CTFontEmphasizedUsage", "CTFontBoldUsage":
    fontName = AppFontName.bold
    case "CTFontObliqueUsage":
    fontName = AppFontName.lightAlt
    default:
    fontName = AppFontName.regular
    }
    self.init(name: fontName, size: fontDescriptor.pointSize)!
    }

    class func overrideInitialize() {
    guard self == UIFont.self else { return }

    if let systemFontMethod = class_getClassMethod(self, #selector(systemFont(ofSize:))),
    let mySystemFontMethod = class_getClassMethod(self, #selector(mySystemFont(ofSize:))) {
    method_exchangeImplementations(systemFontMethod, mySystemFontMethod)
    }

    if let boldSystemFontMethod = class_getClassMethod(self, #selector(boldSystemFont(ofSize:))),
    let myBoldSystemFontMethod = class_getClassMethod(self, #selector(myBoldSystemFont(ofSize:))) {
    method_exchangeImplementations(boldSystemFontMethod, myBoldSystemFontMethod)
    }

    if let italicSystemFontMethod = class_getClassMethod(self, #selector(italicSystemFont(ofSize:))),
    let myItalicSystemFontMethod = class_getClassMethod(self, #selector(myItalicSystemFont(ofSize:))) {
    method_exchangeImplementations(italicSystemFontMethod, myItalicSystemFontMethod)
    }

    if let initCoderMethod = class_getInstanceMethod(self, #selector(UIFontDescriptor.init(coder:))), // Trick to get over the lack of UIFont.init(coder:))
    let myInitCoderMethod = class_getInstanceMethod(self, #selector(UIFont.init(myCoder:))) {
    method_exchangeImplementations(initCoderMethod, myInitCoderMethod)
    }
    }
    }

    AppDelegate.swift
    override init() {
    super.init()
    UIFont.overrideInitialize()
    }

    用法:
    label.font = UIFont.boldSystemFont(ofSize: 16)
    label.fontUIFont.systemFont(ofSize: 12)

    关于ios - 如何以正确的方式在整个 IOS 应用程序中使用自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62316572/

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