我想为我的应用支持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创建一个带分叉的类别?
答案 0 :(得分:11)
我创建了这个扩展程序:
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设置正确的字体。
答案 1 :(得分:5)
使用+ systemFontOfSize:weight:
。适用于iOS 8及更高版本。
对于iOS 7,界面构建器设置将起作用,对于代码,您需要创建具有适当权重的UIFontDescriptor
。
答案 2 :(得分:2)
谢谢@Leo Natan。但我想为复制粘贴爱好者展示一个代码片段。
UIFont* systemFont = [UIFont respondsToSelector:@selector(systemFontOfSize:weight:)] ? [UIFont systemFontOfSize:25 weight:UIFontWeightThin] : [UIFont fontWithName:@"HelveticaNeue-Thin" size:25];
答案 3 :(得分:0)
感谢@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