【问题标题】:System font for both iOS 8 and iOS 9iOS 8 和 iOS 9 的系统字体
【发布时间】:2015-12-17 22:09:36
【问题描述】:

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

【问题讨论】:

    标签: ios ios8 ios9 uifont


    【解决方案1】:

    我已经创建了这个扩展:

    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】:

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

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

      【讨论】:

      • + systemFontOfSize:weight: 适用于 iOS 8.2 及更高版本。
      • @ValentinShamardin 别担心,这个方法从 iOS 8 开始就有了。它只是在 8.2 中作为公共 API 公开,但你可以使用它。
      【解决方案3】:

      感谢@Leo Natan。但我想为复制粘贴爱好者展示一个代码 sn-p。

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

      【讨论】:

      • 最好测试特定功能,而不是操作系统版本。使用[UIFont respondsToSelector:@selector(systemFontOfSize:weight:)] 测试可用性。
      【解决方案4】:

      感谢@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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-30
        • 1970-01-01
        • 2023-04-05
        • 2015-11-14
        • 1970-01-01
        • 1970-01-01
        • 2014-10-22
        • 1970-01-01
        相关资源
        最近更新 更多