iOS 7中苗条的系统字体叫什么?是否有一种方法可以像UIFont systemFontOfSize:
一样使用它?
iOS 8.2之后,您现在可以使用UIFont.systemFontOfSize(_:CGFloat,weight:CGFloat)
:
UIFont.systemFontOfSize(19, weight: UIFontWeightLight)
iOS SDK为字体提供了权重常量:
UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy
当您想要使用系统字体时,使用基于字体名称创建字体不如使用系统字体更好,因为iOS可以更改其系统字体(例如,在iOS 7中使用Helvetica Neue,而现在在iOS 9中使用San Francisco)。
UIFont
的初始化器,即 init?(name fontName: String, size fontSize: CGFloat) -> UIFont
。您需要使用所需字重的字体名称。所有可用的字体名称都在此处列出:http://iosfonts.com。 - chrisamansesystemFontOfSize
来指定一个字体,让操作系统来处理改变。但我现在唯一的问题是,如何在不指定精确字体名称和扩展名(如"HelveticaNeue-Light")的情况下获得一些权重。 - Christian 'fuzi' OrglerUIFont.systemFontOfSize(_:CGFloat,weight:CGFloat)
。 - chrisamansesystemFontOfSize
没有可能指定其他的字重,除了 boldSystemFont
之外... 轻?中等,紧缩等。 - Christian 'fuzi' Orgler只需像下面这样为UIFont创建一个扩展
extension UIFont {
static func lightSystemFontOfSize(size: CGFloat) -> UIFont {
let familyName = UIFont.systemFontOfSize(15).familyName.stringByReplacingOccurrencesOfString(" ", withString: "")
return UIFont(name: "\(familyName)-Light", size: size)!
}
}
刚刚为它创建了一个分类:
#import <UIKit/UIKit.h>
@interface UIFont (System)
+ (UIFont *)lightSystemFontOfSize:(CGFloat)fontSize;
+ (UIFont *)ultraLightSystemFontOfSize:(CGFloat)fontSize;
@end
#import "UIFont+System.h"
@implementation UIFont (System)
+ (UIFont *)lightSystemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:@"HelveticaNeue-Light" size:fontSize];
}
+ (UIFont *)ultraLightSystemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:fontSize];
}
@end