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

前端 未结 13 1507
囚心锁ツ
囚心锁ツ 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:16

    Here is a conversion of Steves answer to Swift code (for quick copy and paste purpose):

        var fontFamilies = UIFont.familyNames()
    
        for (var i:Int = 0; i < fontFamilies.count; i++) {
            var fontFamily: NSString = fontFamilies[i] as NSString
            var fontNames: NSArray = UIFont.fontNamesForFamilyName(fontFamilies[i] as String) as NSArray
    
            NSLog ("%@: %@", fontFamily, fontNames)
        }
    
    0 讨论(0)
  • 2020-11-27 14:21

    This font was not available in 4.1 but it is supported in 4.3. What would be the best way to go about checking if the font exists

    Simply ask for the font in the usual way using UIFont* f = [UIFont fontWithName:... size:...];. If the font isn't available, the result (f) will be nil.

    (One way to guarantee the availability of a font is to include it in the app bundle...)

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

    Swift 5.x

    Forget those for-loops, this is the easiest way to see fonts supported by iOS.

    Just run any of the following in a playground.


    Family Names Only

    Input

    dump(UIFont.familyNames)
    

    Output

    ▿ 75 elements
      - "Copperplate"
      - "Heiti SC"
      - "Kohinoor Telugu"
      ...
    

    Font Names Only

    Input

    dump(UIFont.familyNames.compactMap { UIFont.fontNames(forFamilyName: $0) })    
    

    Output

    ▿ 248 elements
      - "Copperplate-Light"
      - "Copperplate"
      - "Copperplate-Bold"
      ...
    

    Font Names for Specified Family Name

    Input

    dump(UIFont.fontNames(forFamilyName: "Helvetica Neue"))
    

    Output

    ▿ 14 elements
      - "HelveticaNeue-Italic"
      - "HelveticaNeue-Bold"
      - "HelveticaNeue-UltraLight"
      - "HelveticaNeue-CondensedBlack"
      ...
    
    0 讨论(0)
  • 2020-11-27 14:22

    This is what i did on objective c to find out if font is available or not

    NSFont *font = [NSFont fontWithName:@"thefont" size:25.0];
    if (font==nil) {
        // thefont is available
    } else {
        // thefont is not available
    }
    
    0 讨论(0)
  • 2020-11-27 14:26

    [UIFont familyName] is supported back to iPhone OS 2.0 (before 2.0, third-party apps were not allowed on iPhone or iPod touch) , so I would use that to check to see if a font family exists on the current device, and if it doesn't exist, use a suitable fall-back font for that version of iOS. Here's John Gruber's list of iPhone fonts from the original iPhone in 2007 (contrasted with the fonts in Mac OS X of the day). I haven't checked them all, but the iOS fonts I did check are still in iOS 5:

    • http://daringfireball.net/misc/2007/07/iphone-osx-fonts

    Here's an example of using [UIFont familyName]:

    NSLog (@"Font families: %@", [UIFont familyNames]);
    

    This will produce a list like this:

    Font families: ( Thonburi, "Snell Roundhand", "Academy Engraved LET", ... et cetera.

    Once you know the family name, you can use the UIFont class method fontNamesForFamilyName to get an NSArray of the names of all the fonts in the family. For example:

    NSLog (@"Courier New family fonts: %@", [UIFont fontNamesForFamilyName:@"Courier New"]);
    

    That will produce a list like this:

    Courier New family fonts: ( CourierNewPSMT, "CourierNewPS-BoldMT", "CourierNewPS-BoldItalicMT", "CourierNewPS-ItalicMT" )

    The following example code prints a list of each font in every family on the current device:

    NSArray *fontFamilies = [UIFont familyNames];
    
    for (int i = 0; i < [fontFamilies count]; i++)
    {
        NSString *fontFamily = [fontFamilies objectAtIndex:i];
        NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
        NSLog (@"%@: %@", fontFamily, fontNames);
    }
    

    For more information, check out the iOS Developer Reference document for the UIFont class methods familyNames and fontNamesForFamilyName:.

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

    Try to init with that font name, and if it's nil do something else. Swift code:

    if let font = UIFont(name: name, size: size) { // ok } else { // not ok }

    0 讨论(0)
提交回复
热议问题