The basic template for SwiftUI View begins with a simple Text view:
struct ContentView: View {
var body: some View {
Text("Hello World!")
}
}
When a Text view is returned with no attributes, it is put it in the middle of the view.
Command+click on Text and select “Show SwiftUI Inspector…”.
The inspector shows what adjustments can be made to the Text view such as changing the font, weight, or colour. You can also change the alignment, number of lines, and add padding.
Go ahead and make some changes. You will see the attributes appended just after declaring the Text view, as follows:
Text("Hello World!")
.font(.title)
.fontWeight(.thin)
.foregroundColor(Color.red)
.multilineTextAlignment(.center)
.padding(.leading)
Attributes can be added manually. You can also adjust them as needed such as removing .leading from the padding and replacing with an integer. By doing that, all edges are padded to the amount specified by the integer provided.
The Text view centres in the view unless you tell it otherwise. If you embed it in a stack view, it will centre in the stack view and will need to be adjusted as needed.
Leave a Reply
You must be logged in to post a comment.