Krueger
Krueger

Reputation: 484

NSAttributedString with image attachments overlapping text in iOS 16

I have stumbled upon an issue with NSAttributedString and NSTextAttachment in UITextView on iOS16. It works in iOS 15 and appends the images after the text, but in iOS 16 the images are placed on top of the text.

let attributedString = NSMutableAttributedString(string: viewModel.faiModel?.longText ?? "")
attributedString.append(NSAttributedString(string: "\n"))
        
guard let longTexts = viewModel.faiModel?.longTextModels else { return }

for longText in longTexts.sorted() {
     guard let data = longText.binary else { continue }
     let image = UIImage(data: data)
            
     // Create an NSTextAttachment and add image
     let attachment = NSTextAttachment()
     attachment.image = image
            
     // Put your NSTextAttachment into and attributedString and add some spacing
     let attString = NSMutableAttributedString(attachment: attachment)
     attString.appendSpacing(points: 50)
            
     // Add image after the text
     attributedString.append(attString)
}
        
if let attributedText = makeFirstLineHeadline(attributedString) {
   self.textView.attributedText = attributedText
} else {
   self.textView.attributedText = attributedString
}

Here's a screenshot of how it looks in iOS 15 (please disregard that it is different pictures):

enter image description here

And how it looks in iOS 16: enter image description here

I haven't been able to find an official bug report about this for iOS 16. Have any of you experienced the same or have a solution for it?

Upvotes: 2

Views: 263

Answers (0)

Related Questions