I want to support both iOS 8 and iOS 9 systems for my app. And maybe iOS 7. As we know, system font for iOS 7 and 8 is Helvetica Neue. But in iOS 9 system font is San-Francisco. And if you don't set Helvetica font explicitly via [UIFont fontWithName:@"HelveticaNeue" size:15];
, but use [UIFont systemFontOfSize:15];
, you'll get Helvetica for iOS 7 and 8 and San-Francisco for iOS 9 automatically. And it's great!
For interface builder's labels and buttons you can set thin, ultra thin, medium etc. system fonts. It is great too. But how can I set these thin, ultra, medium system fonts in code, programmatically?
Do I need to create a category with a fork for iOS 9 and previous iOS?
Asked
Active
Viewed 4,627 times
6

Valentin Shamardin
- 3,569
- 4
- 34
- 51
4 Answers
11
I've 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)!
}
}
}
Which makes it possible to apply font like this:
myLabel.font = UIFont.systemFontOfSize(14, weight: .Medium)
This will automatically set the correct font for both iOS 8 and iOS 9.

Antoine
- 23,526
- 11
- 88
- 94
5
Use + systemFontOfSize:weight:
. It's available for iOS 8 and above.
For iOS 7, interface builder settings will work, and for code you will need to create a UIFontDescriptor
with the appropriate weight.

Léo Natan
- 56,823
- 9
- 150
- 195
-
3`+ systemFontOfSize:weight:` Available in iOS 8.2 and later. – Valentin Shamardin Sep 21 '15 at 09:46
-
@ValentinShamardin Don't worry about it, the method was there since iOS 8. It was just exposed as public API in 8.2, but you can use it. – Léo Natan Sep 21 '15 at 09:47
2
Thanks @Leo Natan. But I want to show a code snippet for copy-paste lovers.
UIFont* systemFont = [UIFont respondsToSelector:@selector(systemFontOfSize:weight:)] ? [UIFont systemFontOfSize:25 weight:UIFontWeightThin] : [UIFont fontWithName:@"HelveticaNeue-Thin" size:25];

Valentin Shamardin
- 3,569
- 4
- 34
- 51
-
1It is better to test for specific functionality, rather than OS version. Use `[UIFont respondsToSelector:@selector(systemFontOfSize:weight:)]` to test for availability. – Léo Natan Sep 21 '15 at 17:32
0
Thanks @Antoine for posting great answer for swift. Following is Objective C Similar kind of answer if anybody wants. 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

appleBoy21
- 632
- 8
- 23