iOS 8 和 iOS 9 的系统字体

System font for both iOS 8 and iOS 9

我想为我的应用程序同时支持 iOS 8 和 iOS 9 系统。也许 iOS 7。我们知道,iOS 7 和 8 的系统字体是 Helvetica Neue。但是在iOS9系统字体是San-Francisco。如果您没有通过 [UIFont fontWithName:@"HelveticaNeue" size:15]; 显式设置 Helvetica 字体,而是使用 [UIFont systemFontOfSize:15];,您将获得 iOS 7 和 8 的 Helvetica 以及 iOS 9 的 San-Francisco自动地。这太棒了! 对于界面生成器的标签和按钮,您可以设置细、超细、中等系统字体。它也很棒。但是如何以编程方式在代码中设置这些细、超、中等系统字体? 我是否需要为 iOS 9 和之前的 iOS 创建一个带有分支的类别?

使用+ systemFontOfSize:weight:。它适用于 iOS 8 及更高版本。

对于 iOS 7,界面生成器设置将起作用,对于代码,您需要创建具有适当权重的 UIFontDescriptor

谢谢@Leo Natan。但我想为复制粘贴爱好者展示一个代码片段。

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

我创建了这个扩展:

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)!
        }
    }
}

这使得应用这样的字体成为可能:

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

这将自动为 iOS 8 和 iOS 9 设置正确的字体。

感谢@Antoine 为 swift 发布了很好的答案。以下是 Objective C 类似的答案,如果有人想要的话。为 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