How to set text font as System Thin in Swift?

18,539

Solution 1

The system font in the Interface Builder is the OS default font, it is not a font you can get by it's name. For the system font Apple provides the following methods:

+ (UIFont *)systemFontOfSize:(CGFloat)fontSize;
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;
+ (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize;

These do not include any thin version but iOS 8.2 onwards you can use:

+ (UIFont *)systemFontOfSize:(CGFloat)fontSize weight:(CGFloat)weight;

Where you can pass: as weights:

UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy

So a thin system font would be:

UIFont *thinFont = [UIFont systemFontOfSize:15 weight:UIFontWeightThin];

Solution 2

If you're using iOS 8.2 or higher you can use this;

label.font = UIFont.systemFontOfSize(15, weight: UIFontWeightThin)

For previous versions just use HelveticaNeue-Thin as your font.

Edit: for iOS 14 it is:

label.font = UIFont.systemFont(ofSize: 14, weight: .light)
Share:
18,539
Orkhan Alizade
Author by

Orkhan Alizade

Updated on July 21, 2022

Comments

  • Orkhan Alizade
    Orkhan Alizade almost 2 years

    I want to set label text as System Thin. Read in StackOverflow and found an answer like this:

    labelDescriptionView.font = UIFont(name: "System-Thin", size: 15.0)
    

    but it did not work. How can I improve my code and make Thin font style programmatically?

    • Martin R
      Martin R over 8 years
      Where did you find that?
    • Orkhan Alizade
      Orkhan Alizade over 8 years
    • Orkhan Alizade
      Orkhan Alizade over 8 years
      @MartinR oh, I misunderstood the answer =/
    • Dipen Panchasara
      Dipen Panchasara over 8 years
      @OrkhanAlizade its already given, set font name "HelveticaNeue-Thin" as iOS uses "HelveticaNeue" as default font. There is no font specified "System-Thin" in iOS only Bold, Italic & Regular are there for System font.
    • Orkhan Alizade
      Orkhan Alizade over 8 years
      @DipenPanchasara but I can set it to Thin from the File Inspecter
    • rckoenes
      rckoenes over 8 years
      @DipenPanchasara as of iOS 9 HelveticaNeue is no longer the default font!
    • Dipen Panchasara
      Dipen Panchasara over 8 years
      @rckoenes i know its removed in iOS 9 but prior to that its available. Though you can check font availability by iOS version here @OrkhanAlizade to set thin font there is no specific method like boldSystemFontOfSize in iOS, though you can set it explicitly using [UIFont fontWithName:@""] method.
    • Orkhan Alizade
      Orkhan Alizade over 8 years
      I want Thin font, guys =/
    • Dipen Panchasara
      Dipen Panchasara over 8 years
      @OrkhanAlizade set it programatically.
    • Orkhan Alizade
      Orkhan Alizade over 8 years
      @DipenPanchasara my question sounds exactly so. How to set it programmatically?
    • Martin R
      Martin R over 8 years
      @OrkhanAlizade: Did you even try a Google search for "ios system thin font"? It took me less than one minute to find the solution.