Best way to use Font Name using enum in iOS-Swift

Lucky Mehndiratta
2 min readMay 9, 2022

As we need to add the custom font style in our project as per the client’s requirement so rather than using or setting the font name and size from the storyboard inspectors. We can use this way to set the font and size of the text throughout the project. Maybe this will take initial time to set up the project but it gives you a good way to change whenever you need to change as per the requirement.

Benefits?
Once you set your text font name and size of your project then in the future you need to change the font then there is no need to change each and every text UI component one by one. Just replace the name of the brandForntName with your new one and add your new font to the project it will automatically reflect throughout the project.

let brandFontName = “Helvetica” -> your project font name

enum FontName {
case black
case blackItalic
case regular
case medium
case mediumItalic
case semiBold
case semiBoldItalic
case bold
case boldItalic
case extraBold
case extraBoldItalic
case light
case extraLight
case extraLightItalic
case italic
case lightItalic
case thin
case thinItalic

var title: String {

switch self {
case .black: return “\(brandFontName)-Black”
case .blackItalic: return “\(brandFontName)-BlackItalic”
case .regular: return “\(brandFontName)-Regular”
case .medium: return “\(brandFontName)-Medium”
case .mediumItalic: return “\(brandFontName)-MediumItalic”
case .semiBold: return “\(brandFontName)-SemiBold”
case .semiBoldItalic: return “\(brandFontName)-SemiBoldItalic”
case .bold: return “\(brandFontName)-Bold”
case .boldItalic: return “\(brandFontName)-BoldItalic”
case .extraBold: return “\(brandFontName)-ExtraBold”
case .extraBoldItalic: return “\(brandFontName)-ExtraBoldItalic”
case .light: return “\(brandFontName)-Light”
case .extraLight: return “\(brandFontName)-ExtraLight”
case .extraLightItalic: return “\(brandFontName)-ExtraLightItalic”
case .italic: return “\(brandFontName)-Italic”
case .lightItalic: return “\(brandFontName)-LightItalic”
case .thin: return “\(brandFontName)-Thin”
case .thinItalic: return “\(brandFontName)-ThinItalic”
}
}

func with(size: CGFloat) -> UIFont {
return UIFont(name: title, size: size) ?? UIFont.systemFont(ofSize: size)
}
}

Use?
Your Label
@IBOutlet weak var lblTitle: UILabel? {
didSet {
guard let label = lblTitle else { return }
label.font = FontName.bold.with(size: 16)
}
}

Happy Coding!!

--

--