iPhone system font

前端 未结 12 503
日久生厌
日久生厌 2020-12-12 18:31

What is the name of the default system font on the iPhone?

I would like to retrieve this for customizing a UIView.

相关标签:
12条回答
  • 2020-12-12 19:20

    Swift

    Specific font

    Setting a specific font in Swift is done like this:

    let myFont = UIFont(name: "Helvetica", size: 17)
    

    If you don't know the name, you can get a list of the available font names like this:

    print(UIFont.familyNames())
    

    Or an even more detailed list like this:

    for familyName in UIFont.familyNames() {
        print(UIFont.fontNamesForFamilyName(familyName))
    }
    

    But the system font changes from version to version of iOS. So it would be better to get the system font dynamically.

    System font

    let myFont = UIFont.systemFontOfSize(17)
    

    But we have the size hard-coded in. What if the user's eyes are bad and they want to make the font larger? Of course, you could make a setting in your app for the user to change the font size, but this would be annoying if the user had to do this separately for every single app on their phone. It would be easier to just make one change in the general settings...

    Dynamic font

    let myFont = UIFont.preferredFont(forTextStyle: .body)
    

    Ah, now we have the system font at the user's chosen size for the Text Style we are working with. This is the recommended way of setting the font. See Supporting Dynamic Type for more info on this.

    Related

    • Visual List of iOS Fonts
    • How do I make an attributed string using Swift?
    0 讨论(0)
  • 2020-12-12 19:21

    Here is some update for supporting iOS 7. It has Dynamic Font Size now.

    For any and all apps that support “Dynamic Type,” users can select a font size in iOS 7 that works system wide, simply by visiting the "General" section under "Settings" and selecting "Font Size."

    UIFont *dynamicFont  = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    

    And constants list, detailed explanation is here

    NSString *const UIFontTextStyleHeadline;
    NSString *const UIFontTextStyleSubheadline;
    NSString *const UIFontTextStyleBody;
    NSString *const UIFontTextStyleFootnote;
    NSString *const UIFontTextStyleCaption1;
    NSString *const UIFontTextStyleCaption2;
    
    0 讨论(0)
  • 2020-12-12 19:23

    afaik iPhone uses "Helvetica" by default < iOS 10

    0 讨论(0)
  • 2020-12-12 19:24

    You can always use

    UIFont *systemFont = [UIFont systemFontOfSize:12];
    NSLog(@"what is it? %@ %@", systemFont.familyName, systemFont.fontName);
    

    The answer is:

    Up to iOS 6

     Helvetica Helvetica
    

    iOS 7

    .Helvetica Neue Interface .HelveticaNeueInterface-M3
    

    but you can just use Helvetica Neue

    0 讨论(0)
  • 2020-12-12 19:26

    Swift

    You should always use the system defaults and not hard coding the font name because the default font could be changed by Apple at any time.

    There are a couple of system default fonts(normal, bold, italic) with different sizes(label, button, others):

    let font = UIFont.systemFont(ofSize: UIFont.systemFontSize)
    let font2 = UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)
    let font3 = UIFont.italicSystemFont(ofSize: UIFont.systemFontSize)
    

    beaware that the default font size depends on the target view (label, button, others)

    Examples:

    let labelFont = UIFont.systemFont(ofSize: UIFont.labelFontSize)
    let buttonFont = UIFont.systemFont(ofSize: UIFont.buttonFontSize)
    let textFieldFont = UIFont.systemFont(ofSize: UIFont.systemFontSize)
    
    0 讨论(0)
  • 2020-12-12 19:29

    I'm not sure there is an api to get the default system font name. So I just get the name like this :

        //get system default font
        UILabel *label = [[UILabel alloc] init];        
        fontname = label.font.fontName;
        [label release];
    

    Looks stupid but it works.

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