@@ -36,10 +36,10 @@ import AppKit
3636import UIKit
3737#endif
3838
39- #if os(OSX) || os(iOS)
40-
4139public extension AttributedString {
4240
41+ #if os(iOS)
42+
4343 /// Initialize a new text attachment with a remote image resource.
4444 /// Image will be loaded asynchronously after the text appear inside the control.
4545 ///
@@ -61,6 +61,10 @@ public extension AttributedString {
6161 self . init ( attachment: attachment)
6262 }
6363
64+ #endif
65+
66+ #if os(iOS) || os(OSX)
67+
6468 /// Initialize a new text attachment with local image contained into the assets.
6569 ///
6670 /// - Parameters:
@@ -71,8 +75,9 @@ public extension AttributedString {
7175 return nil
7276 }
7377
78+ let image = Image ( named: imageNamed)
7479 let boundsRect = CGRect ( string: bounds)
75- self . init ( image: Image ( named : imageNamed ) , bounds: boundsRect)
80+ self . init ( image: image , bounds: boundsRect)
7681 }
7782
7883 /// Initialize a new attributed string from an image.
@@ -85,20 +90,24 @@ public extension AttributedString {
8590 return nil
8691 }
8792
93+ #if os(OSX)
94+ let attachment = NSTextAttachment ( data: image. pngData ( ) !, ofType: " png " )
95+ #else
8896 var attachment : NSTextAttachment !
8997 if #available( iOS 13 . 0 , * ) {
9098 attachment = NSTextAttachment ( image: image)
9199 } else {
92100 attachment = NSTextAttachment ( data: image. pngData ( ) !, ofType: " png " )
93101 }
102+ #endif
94103
95104 if let bounds = bounds {
96105 attachment. bounds = bounds
97106 }
98107
99108 self . init ( attachment: attachment)
100109 }
110+
111+ #endif
101112
102113}
103-
104- #endif
0 commit comments