Hello SwiftUI learners,
Welcome back to a new issue of Learning SwiftUI. I am back to full code mode, which for me means about 30-45 min of coding every day as that is all I have time for in-between my job and being a full time dad to two wonderful sons.
I am building a onboarding flow at the moment for my app that will help the user track their sport betting results. The app helps the user so they can gain data and insights on where they perform well and where they do not.
While working on the design for the onboarding flow, I realized that I wanted to use justify alignment for the text view that I wanted to present. What I also learned was that this is not possible to do in SwiftUI. Bummer.
These are the options available in SwiftUI:
struct JustifyText: View {
var text = "it is not possible to justify text in SwiftUI for some reason."
var body: some View {
VStack(spacing: 30) {
Text(text)
.multilineTextAlignment(.leading)
Text(text)
.multilineTextAlignment(.trailing)
Text(text)
.multilineTextAlignment(.center)
}
}
}
And what I want to achieve is to justify the text, which is very useful when you are going to display large amount of text. So here is what we will build:
So let’s start by creating a new struct:
struct JustifiedText: UIViewRepresentable {
var text: String
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.text = text
textView.textAlignment = .justified
textView.font = .systemFont(ofSize: 18)
return textView
}
func updateUIView(_ uiView: UITextView, context: Context) {
}
}
This struct is a UIViewRepresentable, which allows you to use UIKit views (UITextView) inside SwiftUI.
• makeUIView(context:): This function creates and returns a UITextView instance.
• UITextView() is initialized.
• textView.text = text assigns the passed text to the text view.
• textView.textAlignment = .justified ensures the text is justified.
• textView.font = .systemFont(ofSize: 18) sets the font size to 18.
• updateUIView(_:context:): This function is supposed to update the UITextView when the SwiftUI state changes. It’s currently empty, meaning the text view won’t update if text changes dynamically.
So with this in place, now lets add some text to our code:
struct JustifyText: View {
var text = "it is not possible to justify text in SwiftUI for some reason. I am also adding more text here as I want to explain how my app works in this onboarding flow. While you are here, you can continue to read as I bet you find this text super interesting. Or not, it actually doesntm atter as I am just trying to show you a way to justify text. Peace and love my friend. Okey I just realized that I need to add more text for visual presentation for this blog post. So I guess I have to come up with more word. Words that are good. What are good words then? I dont know, I am just trying to build an app for the app store. Is is hard to build apps for the app store you might ask, I would say both yes and no. It all depends on how advance your app is. great answer right? Okey I need even more text, bla bla bla I guess. What else can I write about? Today is 2025-03-21 and life is good (Ithink so at least). Weekend is about to start and the weather in sweden is great at the moment. Okey it is only 9 degrees outside but that doesnt matter becayse we have been on -4 degrees for a while so 9 degress feels like summertime now. I actually considered going out today only wearing a t-shirt."
var body: some View {
VStack(spacing: 30) {
JustifiedText(text: text)
}
.padding()
}
}
And voila, you are now done. The text justifies nicely in the view. The good thing about this is that you can use the JustifiedText wherever you want in your project if you want the text to justify in other views as well.
Thanks for reading and have a great day!
/MrSwiftUI