Uifont - How to Get System Thin Font

UIFont - how to get system thin font

You can use system font thin weight:

UIFont.systemFont(ofSize: 34, weight: UIFontWeightThin)

List of available weights for San Francisco:

UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy
UIFontWeightBlack

san francisco font weights

As of iOS 11, UIFontWeight* was renamed to UIFont.Weight.*. More you can get here https://developer.apple.com/documentation/uikit/uifont.weight.

How to set text font as System Thin in Swift?

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];

How to select system font programmatically?

Use .systemFontOfSize which is available in iOS 8.2 and later. For example:

someLabel.font = UIFont.systemFontOfSize(38.0, weight: UIFontWeightSemibold)

UIFont Class Reference

How to check if UIFont is system font?

Here is your method you want:

-(BOOL)isSystemFont:(UIFont *)font
{
return ([[font familyName] isEqualToString:[[UIFont systemFontOfSize:12.0f] familyName]])?YES:NO;
}

Or as an extension in Swift3

extension UIFont {
func isSystemFont() -> Bool {
return self.familyName == UIFont.systemFont(ofSize: 12.0).familyName
}
}

Above method will return as you need

if([self isSystemFont:systemFont1]) NSLog(@"SystemFont");
else NSLog(@"Custom Font");
if([self isSystemFont:customFont1]) NSLog(@"SystemFont");
else NSLog(@"Custom Font");

Output is

2014-03-04 15:48:18.791 TestProject[4031:70b] SystemFont
2014-03-04 15:48:18.791 TestProject[4031:70b] Custom Font

How can I get weight of a UILabel?

To get the font weight string name, use the font descriptor and pass in the face attribute.

Swift 4.2

let font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.bold)
let face = font.fontDescriptor.object(forKey: UIFontDescriptorFaceAttribute) as! String
print("face: \(face)")

Swift 3

let font = UIFont.systemFont(ofSize: 14, weight: UIFontWeightBold)
let face = font.fontDescriptor.object(forKey: UIFontDescriptorFaceAttribute) as! String
print("face: \(face)")


Related Topics



Leave a reply



Submit