How to define UIFont as a constant value in fast

I want to define UIFont as a constant value in swift so that I can reuse it to make it consistent. I used this in Objective-C on #define HelveticaNeue [UIFont fontWithName:@"HelveticaNeue" size:13.0]I want this code to be fast. How can i get this.

I used let to define a constant in swift and works with a string, but does not work with UIFont or any other object.

I also tried other keywords like static, extern, var, etc. without success.

+4
source share
2 answers
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)!
}}

To use it: let font = FontHelper.defaultRegularFontWithSize(15).

+5
source

#define Swift, , , , . let header = UIFont(name: "someFont", size: 20.0)! Swift. , . :

// MARK: Font Struct
struct Font {
    static let header = UIFont(name: "agbookrounded-medium", size: 20.0)!
    static let subheader = UIFont(name: "AvenirLTStd-Black", size: 16.0)!
    static let body1 = UIFont(name: "AvenirLTStd-Roman", size: 14.0)!
    static let body2 = UIFont(name: "AvenirLTStd-Light", size: 10.0)!
}

, :

Font.header

:

Font.header.fontWithSize(32.0)
+4

All Articles