我想在 swift 中将 UIFont 定义为常量值,这样我就可以在应用程序中重用它以使其保持一致。我通过 #define HelveticaNeue [UIFont fontWithName:@"HelveticaNeue"size:13.0]
在 Objective-C 中使用了这个我想要这个代码在 swift 中。我怎样才能得到这个。
我已经使用 let 在 swift 中定义常量,它适用于字符串但不适用于 UIFont 或任何其他对象。
我也尝试过其他关键字,如 static、extern、var 等,但都没有成功。
最佳答案
struct FontHelper {
static func defaultRegularFontWithSize(size: CGFloat) -> UIFont {
return UIFont(name: "SourceSansPro-Regular", size: size)!
}
static func defaultLightFontWithSize(size: CGFloat) -> UIFont {
return UIFont(name: "SourceSansPro-Light", size: size)!
}
static func defaultSemiBoldFontWithSize(size: CGFloat) -> UIFont {
return UIFont(name: "SourceSansPro-Semibold", size: size)!
}}
要使用它:let font = FontHelper.defaultRegularFontWithSize(15)
。
关于swift - 如何在 swift 中将 UIFont 定义为常量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29202857/