Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define UIFont as constant value in swift

Tags:

swift

ios8

uifont

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

I have Used let to define constant in swift and it works on string but not working on UIFont or any other object.

I have also tried other keywords like static, extern,var etc with no success.

like image 957
PatientC Avatar asked Dec 15 '22 15:12

PatientC


1 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).

like image 199
sahara108 Avatar answered Feb 11 '23 21:02

sahara108