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?
6
votes
4 Answers
5
votes
11
votes
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.
2
votes
0
votes
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