System font for iOS 8 and iOS 9

I want to support both iOS 8 and iOS 9 for my application. And, possibly, iOS 7. As you know, the system font for iOS 7 and 8 is Helvetica Neue. But iOS 9 has San Francisco in the system font. And if you do not set the Helvetica font explicitly through [UIFont fontWithName:@"HelveticaNeue" size:15]; but use [UIFont systemFontOfSize:15]; , you will get Helvetica for iOS 7 and 8 and San Francisco for iOS 9 automatically. And that's great! For shortcuts and interface buttons, you can set thin, ultra-thin, medium, etc. System fonts. This is also great. But how can I install these thin, ultra, medium system fonts in code, programmatically? Do I need to create a category using fork for iOS 9 and previous iOS?

+6
source share
4 answers

Use + systemFontOfSize:weight: It is available for iOS 8 and above.

For iOS 7, the interface builder settings will be created, and for the code you will need to create a UIFontDescriptor with the appropriate weight.

+5
source

I created this extension:

 import Foundation import UIKit enum SystemFontWeight : String { case UltraLight = "HelveticaNeue-UltraLight" case Thin = "HelveticaNeue-Thin" case Light = "HelveticaNeue-Light" case Regular = "HelveticaNeue" case Medium = "HelveticaNeue-Medium" case Semibold = "Helvetica-Bold" case Bold = "HelveticaNeue-Bold" case Heavy = "HelveticaNeue-CondensedBold" case Black = "HelveticaNeue-CondensedBlack" var weightValue:CGFloat? { if #available(iOS 8.2, *) { switch self { case .UltraLight: return UIFontWeightUltraLight case .Thin: return UIFontWeightThin case .Light: return UIFontWeightLight case .Regular: return UIFontWeightRegular case .Medium: return UIFontWeightMedium case .Semibold: return UIFontWeightSemibold case .Bold: return UIFontWeightBold case .Heavy: return UIFontWeightHeavy case .Black: return UIFontWeightBlack } } else { return nil } } } extension UIFont { static func systemFontOfSize(fontSize:CGFloat, weight:SystemFontWeight) -> UIFont { if #available(iOS 8.2, *) { return UIFont.systemFontOfSize(fontSize, weight: weight.weightValue!) } else { // Fallback on earlier versions return UIFont(name: weight.rawValue, size: fontSize)! } } } 

That allows you to apply the font as follows:

 myLabel.font = UIFont.systemFontOfSize(14, weight: .Medium) 

This will automatically install the correct font for iOS 8 and iOS 9.

+10
source

Thanks @Leo Natan. But I want to show a code fragment for copy-paste lovers.

 UIFont* systemFont = [UIFont respondsToSelector:@selector(systemFontOfSize:weight:)] ? [UIFont systemFontOfSize:25 weight:UIFontWeightThin] : [UIFont fontWithName:@"HelveticaNeue-Thin" size:25]; 
+2
source

Thanks @Antoine for a great answer for a quick reply. The following is Goal C. A similar answer if someone wants to. Implement Category for UIFont

UIFont + Cat.m

 #import "UIFont+Cat.h" @implementation UIFont (Cat) + (UIFont *)systemFontWithSize:(CGFloat)fontSize weight:(CGFloat)weight { if ([UIFont respondsToSelector:@selector(systemFontOfSize:weight:)]) { return [UIFont systemFontOfSize:fontSize weight:weight]; } NSString *fontName = @"HelveticaNeue"; if (weight == UIFontWeightUltraLight) { fontName = @"HelveticaNeue-UltraLight"; } else if (weight == UIFontWeightThin) { fontName = @"HelveticaNeue-Thin"; } else if (weight == UIFontWeightLight) { fontName = @"HelveticaNeue-Light"; } else if (weight == UIFontWeightRegular) { fontName = @"HelveticaNeue"; } else if (weight == UIFontWeightMedium) { fontName = @"HelveticaNeue-Medium"; } else if (weight == UIFontWeightSemibold) { fontName = @"Helvetica-Bold"; } else if (weight == UIFontWeightBold) { fontName = @"HelveticaNeue-Bold"; } else if (weight == UIFontWeightHeavy) { fontName = @"HelveticaNeue-CondensedBold"; } else if (weight == UIFontWeightBlack) { fontName = @"HelveticaNeue-CondensedBlack"; } return [UIFont fontWithName:fontName size:fontSize]; } @end 
0
source

All Articles