malcommac / SwiftRichString

👩‍🎨 Elegant Attributed String composition in Swift sauce
MIT License
3.1k stars 211 forks source link

Parsing Html List Items #109

Open AmirHossein1372 opened 4 years ago

AmirHossein1372 commented 4 years ago

Hi, I'm trying to parse an html text in a way that each list item (\<li> tag) begins at the new line.

I've tried:

let text = "<ul><li>First</li><li>Second</li><li>Third</li></ul>" let style = Style { $0.color = UIColor.black } let liStyle = Style { $0.color = UIColor.green } let htmlStyle = StyleXML(base: style, ["li": liStyle]) label.attributedText = text.set(style: htmlStyle)

And the output is:

Screen Shot 2020-02-10 at 23 10 10

Is there any way to style them like:

Screen Shot 2020-02-10 at 23 20 21

Thanks, best wishes.

marcusway commented 4 years ago

you could use .textTransforms to do something like

let liStyle = Style {
   ...
   $0.textTransforms = [
       .custom { "\n• " + $0 },
   ]
}

In general, I do think it would be nice to have access to some defaults or out-of-the-box configuration for handling stuff like <li> and <p> tags, which I'm currently wrestling with.