代码之家  ›  专栏  ›  技术社区  ›  Valentin Shamardin

iOS 8和iOS 9的系统字体

  •  6
  • Valentin Shamardin  · 技术社区  · 9 年前

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

    4 回复  |  直到 9 年前
        1
  •  11
  •   Antoine    8 年前

    我已创建此扩展:

    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设置正确的字体。

        2
  •  5
  •   Léo Natan    9 年前

    使用 + systemFontOfSize:weight: 。适用于iOS 8及以上版本。

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

        3
  •  2
  •   Valentin Shamardin    9 年前

    谢谢@利奥·纳坦。但我想向复制粘贴爱好者展示一段代码片段。

    UIFont* systemFont = [UIFont respondsToSelector:@selector(systemFontOfSize:weight:)] ? [UIFont systemFontOfSize:25 weight:UIFontWeightThin] : [UIFont  fontWithName:@"HelveticaNeue-Thin" size:25];
    
        4
  •  0
  •   appleBoy21    7 年前

    感谢@Antoine为swift发布了很棒的答案。以下是目标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