【问题标题】:ios11- UIFont return nilios11-UIFont 返回 nil
【发布时间】:2017-10-09 05:02:53
【问题描述】:

我正在使用 UIFont 更改我的应用程序中的默认字体。该代码在 iOS 10 及更低版本上运行良好,但在 iOS 11 上返回 nil,没有任何改变。我正在使用 swift 4

这是我返回 nil 的代码:

UIFont(name: "AIranianSans", size: 20)

你能帮帮我吗?怎么了 ?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    尝试检查可用的字体

    for familyName:String in UIFont.familyNames {
        print("Family Name: \(familyName)")
        for fontName:String in UIFont.fontNames(forFamilyName: familyName) {
             print("--Font Name: \(fontName)")
         }
    }
    

    可能是字体拼写不同。

    【讨论】:

      【解决方案2】:

      它看起来像一个自定义字体,您必须在 XCode 中添加它作为资源。 XCode 9 存在一个已知问题,即当您通过拖放包含资源时,不会将资源添加到目标。

      在文件检查器中检查 .ttf 文件的目标成员资格。

      【讨论】:

        【解决方案3】:

        https://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/ 遵循此处提到的步骤后,我无法在 swift4 运行时在应用程序中加载字体。当我们在故事板中应用特定的自定义字体时,字体将在运行时出现。那么只有这个api

        UIFont(name: "font name", size: 20.0) 
        

        返回字体实例,否则只会返回 nil。 所以设计时自定义字体工作正常但不是运行时

        要在运行时工作,我们需要加载字体

        加载任意数量的字体文件

        UIFont.registerFontWithFilenameString(filenameString: "font 1 Sans LF Bold.ttf", bundle: Bundle.main)
                UIFont.registerFontWithFilenameString(filenameString: "font 2Sans LF Light.ttf", bundle: Bundle.main)
                UIFont.registerFontWithFilenameString(filenameString: "font 3Sans LF Regular.ttf", bundle: Bundle.main)
                UIFont.registerFontWithFilenameString(filenameString: "font 4Sans LF Medium.ttf", bundle: Bundle.main)
        

        public static func registerFontWithFilenameString(filenameString: String, bundle: Bundle) {

            guard let pathForResourceString = bundle.path(forResource: filenameString, ofType: nil) else {
                print("UIFont+:  Failed to register font - path for resource not found.")
                return
            }
        
            guard let fontData = NSData(contentsOfFile: pathForResourceString) else {
                print("UIFont+:  Failed to register font - font data could not be loaded.")
                return
            }
        
            guard let dataProvider = CGDataProvider(data: fontData) else {
                print("UIFont+:  Failed to register font - data provider could not be loaded.")
                return
            }
        
            guard let fontRef = CGFont(dataProvider) else {
                print("UIFont+:  Failed to register font - font could not be loaded.")
                return
            }
        
            var errorRef: Unmanaged<CFError>? = nil
            if (CTFontManagerRegisterGraphicsFont(fontRef, &errorRef) == false) {
                print("UIFont+:  Failed to register font - register graphics font failed - this font may have already been registered in the main bundle.")
            }
        

        代码被占用 Xcode: Using custom fonts inside Dynamic framework

        【讨论】:

          猜你喜欢
          • 2012-05-02
          • 1970-01-01
          • 1970-01-01
          • 2018-03-14
          • 2013-02-18
          • 2019-06-20
          • 2017-12-16
          • 2014-09-27
          相关资源
          最近更新 更多