skip to Main Content

I’m importing data into my app by use of JSON files. My JSON file has a string attribute like so

"story": "Cristoforo Colombo [nasce](myappurl://action?word=nasce) nel 1451 vicino a Genova, nel nord Italia. A 14 anni [diventa](myappurl://action?word=diventa) marinaio e viaggia in numerosi Paesi. Per Cristoforo Colombo la Terra è rotonda e verso la fine del ‘400, vuole viaggiare verso l’India e vuole farlo con un viaggio verso [ovest](myappurl://action?word=ovest). La [spedizione](myappurl://action?word=spedizione) è costosa e Colombo prima chiede aiuto al re del Portogallo e poi alla regina Isabella di Castiglia. Nel 1492, dopo mesi di navigazione, [scopre](myappurl://action?word=scopre) però un nuovo continente: l’America, che viene chiamata il Nuovo Mondo. Cristoforo Colombo fa altri viaggi in America ma ormai non è più così amato e così [muore](myappurl://action?word=muore) nel 1506 povero e [dimenticato](myappurl://action?word=dimenticato) da tutti.",

I’m using inline hyperlinks i.e. "nasce" to form clickable links in my text once its displayed in Xcode.

In my app, the data is all loaded in correctly from the JSON file. I go to input the above string into a Text view, simple just Text(mySampleString) and the text is displayed on the app but without the links. The inline command to form the links for the word [nasce] etc. does not work and instead it displays the literal characters of "nasce" instead of a hyperlink.

I believe my JSON data is formatted correctly because if I copy the string from the JSON file and just past it directly into the Text() parameter then the links work correctly.

Is there anyway to fix this? or does swiftui just not allow hyperlinks to be formed this way?

2

Answers


  1. you could try using AttributedString, such as:

    Text(try! AttributedString(markdown: mySampleString))
    

    You would put this in a function for convenience.

    Login or Signup to reply.
  2. Make Sure you’re running code in iOS 15 and above. It’ll work without an issue try the below code

    if #available(iOS 15, *) {
       Text(.init(storyDesc))
    }
    

    Output for your reference:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search