UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How to specify line breaks in Text("") ?

Forums > SwiftUI

Is there a way to specify line breaks in Text("") ? @twostraws describes multiline string variables at https://www.hackingwithswift.com/sixty/1/3/multi-line-strings , but I would like to avoid additional variables, and put the line-breaked sentences directly in Text(""). The following does not work :

Text("""
Instructions :                                              // line break
1. Hover to see hint.                                       // line break
2. Scroll mouse wheel up to hear slow pronunciation.        // line break
3. Scroll mouse wheel down to hear fast pronunciation.      // line break
""")  // 😱 Compiles okay, but does not display as intended 😱

Does Swift allow a way to do this?

Thank you.

3      

What are you seeing? Here's how it looks for me:

multiline simulator

4      

You can also do Text("Hello \n World"). The \n new line and \r carriage return have been around a while. Not just common to SwiftUI.

4      

@roosterboy Thanks for checking. I originally thought the syntax was causing the following problem ( only 1 line desplayed, and extra ellipsis is also displayed ) :

But you're right the syntax should work as intended as you tested. I eventually isolated the problem to the other VStack() above. I don't know why this would cause a problem - I will do some more testing and make a new question post if needed. But for now, the good thing is that - after removing the other VStack() - the multiline syntax works as follows :

3      

@GeorgeMCSwifter Thanks, I can use this also.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.