iOS 15 image attachment behaviour to NSAttributedString is different. How can I fix the height of the label?

On iOS 14 when attaching an image to a NSAttributedString the resulting height of the label is correct, however on iOS 15 it is too tall.

iOS 14:

iOS 15:

Code:

view.backgroundColor = .black


label.layer.borderColor = UIColor.red.cgColor

label.layer.borderWidth = 1


let font = UIFont.systemFont(ofSize: 11, weight: .bold)

let text = NSMutableAttributedString(string: "LIVE", attributes: [.foregroundColor: UIColor.systemGreen, .font: font])


let attach = NSTextAttachment()

attach.image = UIImage(named: "live_indicator_image")!

let imageString = NSMutableAttributedString(attachment: attach)

text.append(imageString)


label.attributedText = text

Image:

Xcode version: 13.1

Simulator: iPhone 13 (15.0), iPhone 12 (14.4)

Answered by ForumsContributor in 699085022

I fixed this by setting font attribute to the imageString.

imageString.addAttributes([.font: font], range: NSRange(location: 0, length: imageString.length))

Reference: https://stackoverflow.com/questions/70368520/ios-15-image-attachment-behaviour-to-nsattributedstring-is-different-how-can-i/70408299#70408299

Accepted Answer

I fixed this by setting font attribute to the imageString.

imageString.addAttributes([.font: font], range: NSRange(location: 0, length: imageString.length))

Reference: https://stackoverflow.com/questions/70368520/ios-15-image-attachment-behaviour-to-nsattributedstring-is-different-how-can-i/70408299#70408299

Thx, it is very useful to me.

iOS 15 image attachment behaviour to NSAttributedString is different. How can I fix the height of the label?
 
 
Q