0

I have checked around Stack overflow and online couldnt find any answer for Swiftui. I am manually uploading content to Firestore and fetching it in my app in article format, when i retrieve my data in the app the article comes squished together without line breaks and paragraphs. I tried using \n\n which works in json files, it did not work and came out as text. Below is my fetch function:

func fetchHotspots() {
        let fb = Firestore.firestore()
        
        fb.collection("hotspots").getDocuments { snapshot, error in
            if error == nil {
                if let snapshot = snapshot {
                    DispatchQueue.main.async {
                        self.hotspots = snapshot.documents.map { h in
                            return HotSpots(id: h.documentID,
                                hotid: h["hotid"] as? String ?? "",
                                uid: h["uid"] as? String ?? "",
                                imageUrl: h["imageUrl"] as? String ?? "",
                                name: h["name"] as? String ?? "",
                                location: h["location"] as? String ?? "",
                                countryName: h["countryName"] as? String ?? "",
                                countryFlagUrl: h["countryFlagUrl"] as? String ?? "",
                                phonenumber: h["phonenumber"] as? String ?? "",
                                info: h["info"] as? String ?? "",
                                likes: h["likes"] as? Int ?? 0)
                        }
                        
                    }
                }
            } else {
                
            }
        }
        print("DEBUG: printing hotspots list of \(hotspots)")
    }

The article is in the "info" variable which i have in a TextView below:

VStack(alignment: .center, spacing: 3) {
                    Text(viewModel.hotspot.info)
                        .font(.system(size: 12))
                }

This is an example article that i have in the info variable:

Buckingham Fountain is a Chicago Landmark in the center of Grant Park, and between Queen's Landing and Congress Parkway. Dedicated in 1927, it is one of the largest fountains in the world. The crown jewel of Grant Park is fit for a king — literally. The design was inspired by one of the ornate fountains at the Palace of Versailles in France, built for Louis XV.

It just happens to be double the size of the original, making it one of the largest fountains in the world. The size is meant to symbolize the enormity of nearby Lake Michigan and uses as much as 15,000 gallons of water per minute. At dusk, Buckingham Fountain comes to life. Head to the park after sunset and you’ll be treated to a spectacular light and music show every hour on the hour. If you’re there during the day, don’t miss the hourly, 20-minute water show featuring the fountain’s center jet shooting water 150 feet into the air. The fountain is active from May through mid-October.

Opened to the public in 1927, Buckingham Fountain was commissioned by avid art collector and philanthropist Kate Sturges Buckingham as a memorial for her brother Clarence. The “wedding cake” design was the work of architect Edward H. Bennett, while the fountain’s sculptures — including four sets of Art Deco-style seahorses representing the four states bordering Lake Michigan — were created by French artist Marcel Loyau, who won the Prix National at the 1927 Paris Salon for the project.

It should have 3 paragraghs but instead the firebase output is like this:

Buckingham Fountain is a Chicago Landmark in the center of Grant Park, and between Queen's Landing and Congress Parkway. Dedicated in 1927, it is one of the largest fountains in the world. The crown jewel of Grant Park is fit for a king — literally. The design was inspired by one of the ornate fountains at the Palace of Versailles in France, built for Louis XV. It just happens to be double the size of the original, making it one of the largest fountains in the world. The size is meant to symbolize the enormity of nearby Lake Michigan and uses as much as 15,000 gallons of water per minute. At dusk, Buckingham Fountain comes to life. Head to the park after sunset and you’ll be treated to a spectacular light and music show every hour on the hour. If you’re there during the day, don’t miss the hourly, 20-minute water show featuring the fountain’s center jet shooting water 150 feet into the air. The fountain is active from May through mid-October. Opened to the public in 1927, Buckingham Fountain was commissioned by avid art collector and philanthropist Kate Sturges Buckingham as a memorial for her brother Clarence. The “wedding cake” design was the work of architect Edward H. Bennett, while the fountain’s sculptures — including four sets of Art Deco-style seahorses representing the four states bordering Lake Michigan — were created by French artist Marcel Loyau, who won the Prix National at the 1927 Paris Salon for the project.

EMPIRE
  • 57
  • 7
  • Please update this with a [mre]. It likely doesn't have anything to do with Firebase specifically -- the important thing is the content of the `info` `String`, which you aren't including here. It's also possible that this is a duplicate of https://stackoverflow.com/questions/56482955/adding-unlimited-lines-in-a-text-swiftui – jnpdx Apr 08 '22 at 23:24
  • When I paste your string into a variable and use your included code, it works fine. Things to check 1) Are you *sure* the `String` includes the line breaks you think it does? What happens when you output to the console using `print`? 2) Are your sure that the *only* SwiftUI code necessary to reproduce this is included? Nothing surrounding it effects it? – jnpdx Apr 08 '22 at 23:41
  • @jnpdx its not a duplicate, those questions asked was about line limits. My questions is about Firebase returning paragraghs, like we can achieve by using /n/n in json. – EMPIRE Apr 08 '22 at 23:42
  • I guarantee this doesn't have anything to do with Firebase -- Firebase doesn't arbitrarily remove line breaks from strings. This is about whether your string *actually* does have line breaks in it (see question 1 above) and how SwiftUI is rendering it (see question 2) – jnpdx Apr 08 '22 at 23:45
  • @jnpdx if i include \n\n to show i want another paragraph, Firebase does not output the paragraph instead it shows \n\n as part of the article. I am just trying to find a solution to my problem – EMPIRE Apr 08 '22 at 23:53
  • I think you have a misunderstanding about how strings are encoded -- you would not literally type "\n\n" into the text. Those are human-readable versions of line breaks, which are actually different encoded characters. You *could* search and replace the literal "\n" with an actual line break, but the better solution would be to store strings with actual line breaks. – jnpdx Apr 08 '22 at 23:55
  • @jnpdx uploading manually to Firestore does not give you the option for line breaks, when you tap Enter it saves and exits. On storing strings with actual line breaks is why i asked the question, I do not know how to and would welcome any help in learning how to fix the problem. – EMPIRE Apr 09 '22 at 00:11
  • The best option would be using a program to upload strings with the actual line breaks. If that isn’t a possibility, you could include a set of characters (like “[br]” ) that is unlikely to naturally appear in an article that you dynamically replace in your program and replace with actual line breaks. Do you know how to do that? – jnpdx Apr 09 '22 at 00:38
  • @jnpdx Your idea will be great if i can dynamically replace and get actual line breaks, but I dont know how to do that. – EMPIRE Apr 09 '22 at 00:59
  • `inputString.replacingOccurrences(of: "[br]", with: "\n\n")` – jnpdx Apr 09 '22 at 01:17
  • @jnpdx Thanks for the program, i dont know where to include it in my code, do i include it in the fetch function or in the main struct of the view that has the text? – EMPIRE Apr 09 '22 at 01:26
  • Anywhere that would modify the string. You could do `Text(viewModel.hotspot.info.replacingOccurrences(of: "[br]", with: "\n\n"))`. Keep in mind you have to put those "[br]" characters into Firebase manually. – jnpdx Apr 09 '22 at 01:28

1 Answers1

0

With @jnpdx answer this is the solution to the question:

 Text(viewModel.hotspot.info.replacingOccurrences(of: "[br]", with: "\n\n"))
  

Then go to your articles you upload to Firebase and put "[br]" wherever you want line breaks like below.

Buckingham Fountain is a Chicago Landmark in the center of Grant Park, and between Queen's Landing and Congress Parkway. Dedicated in 1927, it is one of the largest fountains in the world. The crown jewel of Grant Park is fit for a king — literally. The design was inspired by one of the ornate fountains at the Palace of Versailles in France, built for Louis XV. It just happens to be double the size of the original, making it one of the largest fountains in the world. The size is meant to symbolize the enormity of nearby Lake Michigan and uses as much as 15,000 gallons of water per minute. [br]At dusk, Buckingham Fountain comes to life. Head to the park after sunset and you’ll be treated to a spectacular light and music show every hour on the hour. If you’re there during the day, don’t miss the hourly, 20-minute water show featuring the fountain’s center jet shooting water 150 feet into the air. The fountain is active from May through mid-October. Opened to the public in 1927, Buckingham Fountain was commissioned by avid art collector and philanthropist Kate Sturges Buckingham as a memorial for her brother Clarence. [br]The “wedding cake” design was the work of architect Edward H. Bennett, while the fountain’s sculptures — including four sets of Art Deco-style seahorses representing the four states bordering Lake Michigan — were created by French artist Marcel Loyau, who won the Prix National at the 1927 Paris Salon for the project.

EMPIRE
  • 57
  • 7