MobileEntityLogo.png

Hi.

Welcome to the best online resource for IOS & Android tutorials.

How to store custom fonts in SwiftUI

How to store custom fonts in SwiftUI

SwiftUI provides lots of font options by default, however sometimes you need a specific font that isn’t part of the font library. Today I will show you how to create a custom font, and how to store it in a simple function.


enum Style {
case h1
case h2
case h3
}

First we create an enum. Enums make it easy for us to select which custom font template we want to use when we call our myFont function.


func myFont(style:Style) -> Font {
if style == .h1 {

return .custom("Apple Gothic", fixedSize: 32)

}

if style == .h2 {

return .custom("Apple LiGothic", size: 22)

}

if style == .h3 {

return .custom("Apple Casual", fixedSize: 12)

}

return .custom("Apple Gothic", fixedSize: 32)

}

}

As you see our myFont method passes in our enum styles as its parameters, and then returns Font. We then use if statements to select what happens if one of your enums is selected. If an enum case is selected we return our custom font template by returning .custom(fontName, fontSize) when you customize this function you have to enter the font name as a string. You can get the list of apple fonts from here.

Separating vowels from constants in Swift

How to create draggable views with SwiftUI

How to create draggable views with SwiftUI

0