Skip to content

Commit 87bb548

Browse files
committed
#101 Added lazy imageProvider instead of static dictionary in StyleXML
1 parent 64682e8 commit 87bb548

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

Sources/SwiftRichString/Style/StyleGroup.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ public class StyleXML: StyleProtocol {
5454
/// XML Parsing options.
5555
public var xmlParsingOptions: XMLParsingOptions = []
5656

57-
/// Mapped image for memory based images.
58-
public private(set) var imagesMap: [String: UIImage]? = nil
57+
/// Image provider is called to provide custom image when `StyleXML` encounter a `img` tag image.
58+
/// If not implemented the image is automatically searched inside any bundled `xcassets`.
59+
public var imageProvider: ((String) -> UIImage?)? = nil
5960

6061
/// Dynamic attributes resolver.
6162
/// By default the `StandardXMLAttributesResolver` instance is used.
@@ -70,11 +71,9 @@ public class StyleXML: StyleProtocol {
7071
/// - Parameters:
7172
/// - base: base style applied to the entire string.
7273
/// - styles: styles dictionary used to map your xml tags to styles definitions.
73-
/// - images: images used in XML. If a named image is not assigned here the default assets is used (aka `UIImage(named:)`).
74-
public init(base: StyleProtocol? = nil, _ styles: [String: StyleProtocol] = [:], images: [String: UIImage]? = nil) {
74+
public init(base: StyleProtocol? = nil, _ styles: [String: StyleProtocol] = [:]) {
7575
self.styles = styles
7676
self.baseStyle = base
77-
self.imagesMap = images
7877
}
7978

8079
// MARK: - Public Methods

Sources/SwiftRichString/Support/XMLDynamicAttributesResolver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public protocol XMLDynamicAttributesResolver {
7676
extension XMLDynamicAttributesResolver {
7777

7878
public func imageWithName(_ name: String, fromStyle style: StyleXML) -> UIImage? {
79-
guard let mappedImage = style.imagesMap?[name] else {
79+
guard let mappedImage = style.imageProvider?(name) else {
8080
return UIImage(named: name) // xcassets fallback
8181
}
8282

0 commit comments

Comments
 (0)