0

I'm using firebase to store strings and display them as LocalizedString Text in my SwiftUI view.

Problem is newline characters \n are not being recognized.

Below is the code, can you help :

Firebase string field i.e learningcontent field

This is the first line \n this is the second line

ViewModel :

self.learningContent = LocalizedStringKey(String(document.get("learningcontent") as? String ?? "" ))

Swiftui code :

Text(learningContent) .lineSpacing(10)

View Output : This is the first line \n this is the second line

Problem : \n is not recognized and text is only displayed in one line.

String displayed over multiple lines

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52

1 Answers1

0

Use the below code for allowing multiline in text

Text(learningContent)
   .lineLimit(nil)

here instead of 'nil' you can any number which represents the maximum number of lines allowed to create.

If these don't work for you then maybe you have some special case sometimes \n don't work in the below case

"Hello World \n This is a new line" 

but if you remove space after \n maybe work

"Hello World \nThis is a new line"

In some cases \r\n may work.

For more detail you can refer to the link: How do I make a new line in swift

Jatin Bhuva
  • 1,301
  • 1
  • 4
  • 22