I found that there are two ways to create an NSTextAttachment
of an image:
// Way 1:
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "...")
// Way 2:
let attachment = NSTextAttachment(image: UIImage(named: "...")!)
I thought these two ways are totally the same, until I did it with SF Symbols' images:
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(systemName: "photo")!
let fullString = NSMutableAttributedString(string: "1: Press the ")
fullString.append(NSAttributedString(attachment: imageAttachment))
fullString.append(NSAttributedString(string: " button"))
textview.attributedText = fullString
produces:
whereas:
let imageAttachment = NSTextAttachment(image: UIImage(systemName: "photo")!)
let fullString = NSMutableAttributedString(string: "1: Press the ")
fullString.append(NSAttributedString(attachment: imageAttachment))
fullString.append(NSAttributedString(string: " button"))
textview.attributedText = fullString
produces:
I thought maybe way 2 "preserves colours" or something like that, but if I use my own image, both ways produce the same result.
Both of their documentations (1, 2) don't say anything about how they are different. Both seems to say the same thing...
So what does NSTextAttachment(image:)
do, that .image = ...
doesn't? Or, what does .image = ...
do, that NSTextAttachment(image:)
doesn't? Are there other images (other than SF symbol images) that I can see a difference?
I'm running this on Xcode 11.0 and iOS 13.0, so it might be a bug of a non-latest version of iOS?