How to create Text component with formats in SwiftUI

Last updated Dec 18, 2021

As you learnt in previous tutorial to create a project in SwiftUI and how to view the Preview of our code. If not, just go through before continuing this tutorial. Create SwiftUI project in Xcode

 

SwiftUI provides the views and controls in the sameway as UIKit to present our content on the screen and as well to handle the user interactions.

 

Every custom view in SwiftUI conforms to the View Protocol. A type that represents part of your app’s user interface and provides modifiers that you use to configure views.

 

The View protocol provides vide variety of modifiers that can be used to change the appearance, behaviour, position and interactions of the controls or views. For example font, opacity, padding, respond to taps, gestures, animations, transitions etc.

 

You can use custom fonts as well

Similarly, you can use .italic(), .opacity(0.5), multilineTextAlignment(.center), etc., As the names indicates, the text will be formated in italic, applied half of the opacity and alignment is to be centered respectively.

 

For your reference, you can download the example project code in the below which has few texts and the formats. In the example project, I have used layouts to format multiple Text components. We are going to discuss in detail on those layouts (HStack, VStack and ZStack) in upcoming articles.

 

Output:

 

Conclusion:

In this tutorial you learnt how to create Text components with the formats like how we do Label using UIKit. In next tutorial, you will learn how to use TextField, SecureField and TextEditor with interacted preview.

 

Download Source code

 

Create First SwiftUI project in Xcode

How do i create Textfield and Secure Texfield in SwiftUI

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

544 Views