简体   繁体   中英

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 attachment = NSTextAttachment()

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

let imageString = NSMutableAttributedString(attachment: attachment)

text.append(imageString)


label.attributedText = text

Image:

在此处输入图像描述

Xcode version: 13.1

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

I have similar problem on my project that displayed well on iOS14 or former but wrong on iOS15. In order to fix this, I added font attribute to the NSMutableAttributedString made with NSTextAttachment right before appending to the final text as below. Please try.

let attachment = NSTextAttachment()

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

let imageString = NSMutableAttributedString(attachment: attachment)

imageString.addAttribute(.font, value: font, range: imageString.length)

text.append(imageString)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM