swift - 如何在 swift 中将 UIFont 定义为常量值

标签 swift ios8 uifont

我想在 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/

相关文章:

ios - 从类 'NSObject' 和 'AWSDynamoDBObjectModel' 多重继承

ios - 在 StackView 中的 UIImage 上添加 UILabel

ios - collectionview 只填充 iPhone 4-5 上的一个单元格

ios - 在 Widget/Today View Extension 中使用核心数据模型?

ios - 委托(delegate)函数的顺序在 iOS 中重要吗?

ios - 将字体大小设置为整数?

ios - NSFont 和 UIFont 有什么区别

arrays - 是否可以在 swift 2.2 中为整数数组构建通用转换?

ios - iOS 中的字母间距

iOS8 触摸位置在风景中受到限制,好像窗口在一侧是肖像?