SwiftUI — Text

Creative mind developer
2 min readJan 1, 2021

If you’ve worked with UIKit before, the Text control in SwiftUI is very similar to UILabel in UIKit. It’s a view for you to display one or multiple lines of text. This Text control is non-editable but is useful for presenting read-only information on screen

Let’s start how we can display simple text using SwiftUI.

Text(“Hello World”)

All programming language start with “Hello world” so we can also use it..🤣

In SwiftUI if you want to change the properties (Ex: color, shadow, etc) of a control by calling methods that are known as Modifiers.

for example if we have to set bold text then in SwiftUI we can set it directly.

Text(“Hello World”).fontWeight(.bold)

SwiftUI provide more direct control compare to Swift and objective C. You can also use the font modifier to specify the font design. you can use rounded font. You can write the font modifier like this:

.font(.system(.title, design: .rounded))

Here we specify to use system font with title text style with rounded font style.

If you want to use other font then you have to use below code.

.font(.custom("Helvetica Neue", size: 17))

Text control by default provide multi line text. without using any modifier text displaying in multi line.

Text("Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.")
.font(.title)
.foregroundColor(.gray)

To align the text you can use multilineTextAlignment modifier and set the value.

if you wan to set line limit then you use below code.

.lineLimit(5)

By default, the system is set to use tail truncation. To modify the truncation mode of the text, you can use the truncationMode modifier and set its value

Thanks again for reading and have a great day!

--

--

Creative mind developer
0 Followers

I am working as a mobile application developer. I generally working on chat application and real time communication applications