psharanda / Atributika

Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.
MIT License
1.45k stars 155 forks source link

NSAttributedString format support? #165

Closed Gargo closed 1 year ago

Gargo commented 1 year ago

How to do this with your library?

let formatString: NSAttributedString = ... //some string with format "%1$@ ... %2$@ ... %3$@"
let param1: NSAttributedString = ...
let param2: NSAttributedString = ...
let param3: NSAttributedString = ...

NSAttributedString(format: formatString, param1, param2, param3)
psharanda commented 1 year ago

Hey, If I understand correctly you would like to style substrings. In that case you can do it like this:

let str = String(format: "<red>%1$@</red>, <blue>%2$@</blue>", "Red Arg", "Blue Arg")
        .style(tags: ["red": Attrs().foregroundColor(.red), "blue": Attrs().foregroundColor(.blue)])
        .attributedString