Skip to content

Commit 408d9df

Browse files
committed
#98 Fixed a bug in UIKit which prevents rendering UIImage instances in original form
1 parent 30b682e commit 408d9df

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Sources/SwiftRichString/Extensions/AttributedString+Attachments.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,16 @@ public extension AttributedString {
9595
#else
9696
var attachment: NSTextAttachment!
9797
if #available(iOS 13.0, *) {
98-
attachment = NSTextAttachment(image: image)
98+
// Due to a bug (?) in UIKit we should use two methods to allocate the text attachment
99+
// in order to render the image as template or original. If we use the
100+
// NSTextAttachment(image: image) with a .alwaysOriginal rendering mode it will be
101+
// ignored.
102+
if image.renderingMode == .alwaysTemplate {
103+
attachment = NSTextAttachment(image: image)
104+
} else {
105+
attachment = NSTextAttachment()
106+
attachment.image = image.withRenderingMode(.alwaysOriginal)
107+
}
99108
} else {
100109
attachment = NSTextAttachment(data: image.pngData()!, ofType: "png")
101110
}

0 commit comments

Comments
 (0)