@@ -38,6 +38,16 @@ import UIKit
3838// MARK: - XMLDynamicAttributesResolver
3939
4040public protocol XMLDynamicAttributesResolver {
41+
42+ /// When an `img` tag is found this function is called to return requested image.
43+ /// Default implementation of this method receive the `name` attribute of the `img` tag along with any mapping
44+ /// provided by calling `StyleXML`, if `mapping` does not contains requested image for given name
45+ /// the `UIImage(named:)` is called and image is searching inside any bundled `xcasset` file.
46+ ///
47+ /// - Parameters:
48+ /// - name: name of the image to get.
49+ /// - fromStyle: caller instance of `StyleXML.
50+ func imageWithName( _ name: String , fromStyle style: StyleXML ) -> UIImage ?
4151
4252 /// You are receiving this event when SwiftRichString correctly render an existing tag but the tag
4353 /// contains extra attributes you may want to handle.
@@ -47,7 +57,8 @@ public protocol XMLDynamicAttributesResolver {
4757 /// - Parameters:
4858 /// - attributedString: attributed string. You will receive it after the style is applied.
4959 /// - xmlStyle: xml style information with tag, applied style and the dictionary with extra attributes.
50- func applyDynamicAttributes( to attributedString: inout AttributedString , xmlStyle: XMLDynamicStyle )
60+ /// - fromStyle: caller instance of `StyleXML.
61+ func applyDynamicAttributes( to attributedString: inout AttributedString , xmlStyle: XMLDynamicStyle , fromStyle: StyleXML )
5162
5263 /// You will receive this event when SwiftRichString can't found a received style name into provided group tags.
5364 /// You can decide to handle it. The default receiver for example uses the `a` tag to render passed url if `href`
@@ -57,15 +68,29 @@ public protocol XMLDynamicAttributesResolver {
5768 /// - tag: tag name received.
5869 /// - attributedString: attributed string received.
5970 /// - attributes: attributes of the tag received.
60- func styleForUnknownXMLTag( _ tag: String , to attributedString: inout AttributedString , attributes: [ String : String ] ? )
71+ /// - fromStyle: caller instance of `StyleXML.
72+ func styleForUnknownXMLTag( _ tag: String , to attributedString: inout AttributedString , attributes: [ String : String ] ? , fromStyle: StyleXML )
6173
6274}
6375
76+ extension XMLDynamicAttributesResolver {
77+
78+ public func imageWithName( _ name: String , fromStyle style: StyleXML ) -> UIImage ? {
79+ guard let mappedImage = style. imagesMap ? [ name] else {
80+ return UIImage ( named: name) // xcassets fallback
81+ }
82+
83+ // origin xml style contains mapped image.
84+ return mappedImage
85+ }
86+
87+ }
88+
6489// MARK: - StandardXMLAttributesResolver
6590
6691open class StandardXMLAttributesResolver : XMLDynamicAttributesResolver {
6792
68- public func applyDynamicAttributes( to attributedString: inout AttributedString , xmlStyle: XMLDynamicStyle ) {
93+ public func applyDynamicAttributes( to attributedString: inout AttributedString , xmlStyle: XMLDynamicStyle , fromStyle : StyleXML ) {
6994 let finalStyleToApply = Style ( )
7095 xmlStyle. enumerateAttributes { key, value in
7196 switch key {
@@ -80,7 +105,7 @@ open class StandardXMLAttributesResolver: XMLDynamicAttributesResolver {
80105 attributedString. add ( style: finalStyleToApply)
81106 }
82107
83- public func styleForUnknownXMLTag( _ tag: String , to attributedString: inout AttributedString , attributes: [ String : String ] ? ) {
108+ public func styleForUnknownXMLTag( _ tag: String , to attributedString: inout AttributedString , attributes: [ String : String ] ? , fromStyle : StyleXML ) {
84109 let finalStyleToApply = Style ( )
85110 switch tag {
86111 case " a " : // href support
@@ -99,8 +124,9 @@ open class StandardXMLAttributesResolver: XMLDynamicAttributesResolver {
99124 #if os(iOS) || os(OSX)
100125 // Local Image support
101126 if let imageName = attributes ? [ " named " ] {
102- if let image = AttributedString ( imageNamed: imageName, bounds: attributes ? [ " rect " ] ) {
103- attributedString. append ( image)
127+ if let image = imageWithName ( imageName, fromStyle: fromStyle) ,
128+ let imageString = AttributedString ( image: image, bounds: attributes ? [ " rect " ] ) {
129+ attributedString. append ( imageString)
104130 }
105131 }
106132 #endif
0 commit comments