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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.