malcommac / SwiftRichString

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

How to parse xml tags (was img not working when use a XML tag) #100

Closed lucasmpaim closed 4 years ago

lucasmpaim commented 4 years ago

Current behaviour: Now, the tag are raw added on text, like:

wrong behaviour

Expected bahaviour: The correct bahaviour is change the "img" tag by correct image, like:

Correct behaviour

Code:

"""
- Use maiúsculas e minúsculas <img named="ic_blue_check_filter"/>
"""
            .set(style: Style {
                $0.font = NAVIGATION_FONT.withSize(14)
                $0.color = UIColor(hex: "#8E8E8E")
            })
malcommac commented 4 years ago

You should use StyleGroup (it will be renamed to StyleXML into the next version) in order to parse XML tags, including built-in img tag.

So for your example:

    let text = """
                    - Use maiúsculas e minúsculas <img named="therocket"/>
                    """

    let baseStyle = Style {
      $0.font = UIFont.boldSystemFont(ofSize: 14)
      $0.color = UIColor(hexString: "#8E8E8E")
    }
    let styleXML = StyleXML(base: baseStyle, [:])
    self.textView?.attributedText = text.set(style: styleXML)