Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set text font as System Thin in Swift? [duplicate]

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?

like image 620
Orkhan Alizade Avatar asked Nov 30 '15 13:11

Orkhan Alizade


People also ask

How do I change the font style in Swift?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.


2 Answers

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)
like image 99
ujell Avatar answered Sep 28 '22 04:09

ujell


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];
like image 38
rckoenes Avatar answered Sep 28 '22 05:09

rckoenes