How To Change Font Style In SwiftUI

Changning font style is one of the basic operations on text, in SwiftUI it’s really easy.

There is some Text view.

Text(“Smash Swift!”)

And you want to make it bold, there is .bold view modifier.

Text(“Smash Swift!”)
    .
bold()

To make it italic there is .italic view modifier.

Text(“Smash Swift!”)
    .
italic()

To underline there is .underline view modifier.

Text(“Smash Swift!”)
    .
underline()

Underline has also the option to change color and turn it off and on.

Text(“Smash Swift!”)
    .
underline(true, color: .red)

To cross the text there is .strikethrough.

Text(“Smash Swift!”)
    .
strikethrough()

It has the same option as underline, to turn it off and on and set line color.

Text(“Smash Swift!”)
    .
strikethrough(true, color: .green)

To change space between letters, there are .kerning and .tracking view modifiers. The difference is in ligatures.

Text(“Smash Swift!”)
    .
kerning(10)

or

Text(“Smash Swift!”)
    .
tracking(-5)

You can also use the .fontWeight view modifier. It allows for example make text thinner.

Bold text:

Text(“Smash Swift!”)
    .
fontWeight(.bold)

Thin text:

Text(“Smash Swift!”)
    .
fontWeight(.thin)