NSTextAttachment图像中具有前景色的属性文本

问题描述 投票:0回答:1

当我将图像附件添加到具有前景色设置的UITextView时,图像将被设置为空白:

enter image description here

let attrString = NSMutableAttributedString(string: rawText, attributes: [.font: UIFont.systemFont(ofSize: 17), .foregroundColor: UIColor.black])
let attachment = NSTextAttachment(image: image)
let imgStr = NSMutableAttributedString(attachment: attachment)
attrString.append(imgStr)
textview.attributedText = attrString

当我删除.foregroundColor: UIColor.black时,图像可以正确显示,但是我需要能够设置属性的文字颜色。

我尝试添加图像附件后运气不佳,但明确地删除了.foregroundColor属性。我还尝试从大部分文本中删除.foregroundColor属性,但仍然无法正常工作,仅从整个字符串中删除该属性即可:

attrString.removeAttribute(.foregroundColor, range: NSRange(location: attrString.length-1, length: 1)) // does not work
// -------
attrString.removeAttribute(.foregroundColor, range: NSRange(location: 1, length: attrString.length-1)) // does not work
// -------
attrString.removeAttribute(.foregroundColor, range: NSRange(location: 0, length: attrString.length)) // works but no text colour

这是在iOS的Xcode 11.0上开发的。这是UITextView / iOS的错误还是预期的行为(我认为不太可能)?如何正确设置带有文字颜色的图像?

swift nsattributedstring nstextattachment
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.