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

Styling different pieces of json text

Forums > SwiftUI

I have a json file the that has a long description object. The description text needs to be styled. Can this be done in Swiftui? If so, how?

It is fairly easy in HTML:

[                               
    { "imageurl": "1.png",
    "levelLongDesc":"A longer description",
    "id": "1.",
    "name": "A name)",
    "page": "Details",
    "description":"<p><b>E pluribus unum</b></p><b>Instructions.</b> Latin for “Out of many one”, is a motto requested by Pierre Eugene du Simitiere (originally Pierre-Eugène Ducimetière) and found in 1776 on the Seal of the United States, along with Annuit cœptis and Novus ordo seclorum, and adopted by an Act of Congress in 1782.</p><p>",
    "videoDemo":"myvideo"
    }...]

TIA-Curiosity

4      

So are you reading in an HTML fragment from the JSON? Unfortunately, NSAttributedString doesn't conform to the Codable protocol so you can't just throw it at a JSONDecoder and get the result you want. Instead, you have to read in the styled string as data when you are decoding the JSON object, then use NSAttributedString's init(data:options:documentAttributes:) to create a styled string from the HTML.

I'll see about working up some code this weekend to demonstrate if nobody else gets to it first.

3      

Thank you for your reply. It is nice to know that there is a way to use the html formated json. I'm so new to trying to learn swift that I probably won't understand, but I look forward to seeing your solution.

Is there any way to actually style the text within the description using Swiftui? Just curious about that. I am using Cordova for my apps now. I'd like to move away from it. Some things about Swiftui are great...others, are quite a challenge.

Thanks again for your help.

3      

I also wondered if I should use Core Data rather than a local json file? What do you think?

3      

I have the exact same question as @ValiantCuriosity regarding styling JSON. I've come up empty handed in my searching so far.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.