malcommac / SwiftRichString

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

User memory-based instances of UIImage in StyleXML #101

Closed malcommac closed 4 years ago

malcommac commented 4 years ago

It would be good to use passed in-memory instances of UIImage objects used when parsing StyleGroup/StyleXML images. If mapping is not resolved the default fallback should be the any compiled .xcassets file.

malcommac commented 4 years ago

The XMLDynamicAttributesResolver class now provides an additional method called:

func imageWithName(_ name: String, fromStyle style: StyleXML) -> UIImage?

The default implementation of this method:

This is an example:

let text = """
- <img named="check"/> Performed!
"""
let base = Style {
  $0.font = UIFont.boldSystemFont(ofSize: 14)
  $0.color = UIColor(hexString: "#8E8E8E")
}

let xmlStyle = StyleXML(base: base)
xmlStyle.imageProvider = { imageName in
  switch imageName {
   case "check":
     return UIColor.red.image(CGSize(width: 30, height: 30))
  default:
    // ...
  }
}

self.textView?.attributedText = text.set(style: x)