How to check if a font is available in version of iOS?

前端 未结 13 1509
囚心锁ツ
囚心锁ツ 2020-11-27 14:02

I\'m currently working on an app that uses the \"ChalkboardSE-Regular\" font and my deployment target is 4.0+. This font was not available in 4.1 but it is supported in 4.3

相关标签:
13条回答
  • 2020-11-27 14:28

    print("Font families: %@", UIFont.familyNames)

    0 讨论(0)
  • 2020-11-27 14:29

    For iOS9 / Swift 2.0, none of above won't work as the syntax changed a little. I also personally prefer to use extension (I choose to create one for UIFont, as it fits the best and modified @API answer as this was the best one):

    extension UIFont {
    
        static func availableFonts() {
    
            // Get all fonts families
            for family in UIFont.familyNames() {
                NSLog("\(family)")
    
                // Show all fonts for any given family
                for name in UIFont.fontNamesForFamilyName(family) {
                    NSLog("   \(name)")
                }
            }
        }
    } 
    

    Now you can just call:

    UIFont.availableFonts()
    

    And it will tell you all the fonts in the form of

    : Bangla Sangam MN
    :    BanglaSangamMN-Bold
    :    BanglaSangamMN
    : Zapfino
    :    Zapfino
    

    Hope it helps!

    0 讨论(0)
  • 2020-11-27 14:30

    For objective-c

    for (NSString *family in UIFont.familyNames) {
        NSLog(@"family %@",family);
        for (NSString *name in [UIFont fontNamesForFamilyName:family]) {
            NSLog(@"      name = %@",name);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 14:31

    Swift 4.x

    UIFont.familyNames().sort( { $0 < $1 } ).forEach({ print("\($0)"); UIFont.fontNamesForFamilyName("\($0)").sort( { $0 < $1 } ).forEach({ print("     \($0)") }) })
    
    0 讨论(0)
  • 2020-11-27 14:39

    If you use Swift you can print all fonts (see below). You can also check if the font exists.

    for family in UIFont.familyNames {
        print("\(family)")
    
        for name in UIFont.fontNames(forFamilyName: family) {
            print("\(name)")
        }
    }
    
    0 讨论(0)
  • 2020-11-27 14:39

    Swift version:

    UIFont.familyNames().sort( { $0 < $1 } ).forEach({ print("\($0)"); UIFont.fontNamesForFamilyName("\($0)").sort( { $0 < $1 } ).forEach({ print("     \($0)") }) })
    
    0 讨论(0)
提交回复
热议问题