malcommac / SwiftRichString

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

Question: Handling multiple/nested tags #121

Open marcusway opened 4 years ago

marcusway commented 4 years ago

Hello!

  1. Thanks for this library; it's great.
  2. I'm wondering what the best way is for dealing with with nested tags. for instance, if i have <strong><em>Hello!</em></strong>, I'd like for "Hello!" to be both bolded and italicized, but the most deeply nested tag wins the day with my current approach (i.e. the hello example is italicized but not bolded). This is what I have:
let baseStyle = Style {...}

let boldStyle = baseStyle.byAdding {
    $0.traitVariants = $0.traitVariants?.union(.bold) ?? .bold;
    // have also tried $0.traitVariants = .bold, same effect
}

let italicStyle = baseStyle.byAdding {
    $0.traitVariants = $0.traitVariants?.union(.italic) ?? .italic
    // have also tried $0.traitVariants = .italic, same effect

}

let xmlStyle = SyleXML(base: baseStyle, ["strong": strongStyle, "em": italicStyle])

I'm a bit of an iOS noob; any direction here is much appreciated.

Thanks again!