Chapters

Hide chapters

SwiftUI Cookbook

Live Edition · iOS 16.4 · Swift 5.8.1 · Xcode 14.3.1

Create a Text View in SwiftUI
Written by Team Kodeco

In SwiftUI, you can display text on the screen simply by creating an instance of the Text view and providing it with some string content.

Here’s an example of the Text view in action:

struct ContentView: View {
  var body: some View {
    Text("Hello, World!")
  }
}

In the code above, you create a ContentView struct that returns a Text view with the string “Hello, World!”. This view will display that message on the screen when it is rendered.

SwiftUI Text view with title font.
SwiftUI Text view with title font.

You can customize the properties of the Text view to change the font, color and other characteristics. For example, you can change the font and size of the text by using the font modifier, like this:

Text("Hello, World!")
  .font(.title)

This styles the text to the built-in .title text style, which is larger than the default font size.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.