NSTextAttachment.bounds 不适用于 iOS 15
NSTextAttachment.bounds does not work on iOS 15
为了向属性字符串添加图像附件,我使用了以下在 iOS 15 之前运行良好的代码:
let textAttachment = NSTextAttachment()
textAttachment.image = UIImage(named:"imageName")!
textAttachment.bounds = CGSize(origin: CGPoint(x: x, y: y), size: CGSize(width: width, height: height))
let textAttachmentString = NSMutableAttributedString(attachment: textAttachment)
let attributedText = NSMutableAttributedString(string: "base text", attributes: [:])
attributedText.append(textAttachmentString)
问题是iOS15后textAttachment.bounds.origin.x
不行:text/image之间没有space,附件原点不变
可能应该使用NSTextAttachmentViewProvider
,但我不知道如何进行。
不久前,我用一个不优雅的解决方法解决了这个问题,我写了一个函数,将一个空文本附件插入到内容中,以在文本和图标之间创建一个水平 space:
func addIconToText(_ toText: NSAttributedString, icon: UIImage, iconOrigin: CGPoint, iconSize: CGSize, atEnd: Bool)->NSAttributedString{
// Inserts a textAttachment containing the given icon into the attributed string (at the beginning or at the end of the text depending on the value of atEnd)
// Constructs an empty text attachment to separate the icon from the text instead of using NSTextAttachment.bounds.origin.x because it does not work with iOS 15
let iconTextAttachment = NSTextAttachment(data: icon.withRenderingMode(.alwaysOriginal).pngData(), ofType: "public.png")
iconTextAttachment.bounds = CGRect(origin: CGPoint(x: 0, y: iconOrigin.y), size: iconSize)
let iconString = NSMutableAttributedString(attachment: iconTextAttachment)
let emptyIconSize = CGSize(width: abs(iconOrigin.x), height: abs(iconOrigin.y))
let emptyIconTextAttachment = NSTextAttachment(image: UIGraphicsImageRenderer(size: emptyIconSize).image(actions: {
con in
UIColor.clear.setFill()
con.fill(CGRect(origin: .zero, size: emptyIconSize))
}))
emptyIconTextAttachment.bounds = CGRect(origin: .zero, size: emptyIconSize)
let emptyIconString = NSMutableAttributedString(attachment: emptyIconTextAttachment)
var finalString: NSMutableAttributedString!
if atEnd{
finalString = NSMutableAttributedString(attributedString: toText)
finalString.append(emptyIconString)
finalString.append(iconString)
}else{
finalString = NSMutableAttributedString(attributedString: iconString)
finalString.append(emptyIconString)
finalString.append(toText)
}
return finalString
}
为了向属性字符串添加图像附件,我使用了以下在 iOS 15 之前运行良好的代码:
let textAttachment = NSTextAttachment()
textAttachment.image = UIImage(named:"imageName")!
textAttachment.bounds = CGSize(origin: CGPoint(x: x, y: y), size: CGSize(width: width, height: height))
let textAttachmentString = NSMutableAttributedString(attachment: textAttachment)
let attributedText = NSMutableAttributedString(string: "base text", attributes: [:])
attributedText.append(textAttachmentString)
问题是iOS15后textAttachment.bounds.origin.x
不行:text/image之间没有space,附件原点不变
可能应该使用NSTextAttachmentViewProvider
,但我不知道如何进行。
不久前,我用一个不优雅的解决方法解决了这个问题,我写了一个函数,将一个空文本附件插入到内容中,以在文本和图标之间创建一个水平 space:
func addIconToText(_ toText: NSAttributedString, icon: UIImage, iconOrigin: CGPoint, iconSize: CGSize, atEnd: Bool)->NSAttributedString{
// Inserts a textAttachment containing the given icon into the attributed string (at the beginning or at the end of the text depending on the value of atEnd)
// Constructs an empty text attachment to separate the icon from the text instead of using NSTextAttachment.bounds.origin.x because it does not work with iOS 15
let iconTextAttachment = NSTextAttachment(data: icon.withRenderingMode(.alwaysOriginal).pngData(), ofType: "public.png")
iconTextAttachment.bounds = CGRect(origin: CGPoint(x: 0, y: iconOrigin.y), size: iconSize)
let iconString = NSMutableAttributedString(attachment: iconTextAttachment)
let emptyIconSize = CGSize(width: abs(iconOrigin.x), height: abs(iconOrigin.y))
let emptyIconTextAttachment = NSTextAttachment(image: UIGraphicsImageRenderer(size: emptyIconSize).image(actions: {
con in
UIColor.clear.setFill()
con.fill(CGRect(origin: .zero, size: emptyIconSize))
}))
emptyIconTextAttachment.bounds = CGRect(origin: .zero, size: emptyIconSize)
let emptyIconString = NSMutableAttributedString(attachment: emptyIconTextAttachment)
var finalString: NSMutableAttributedString!
if atEnd{
finalString = NSMutableAttributedString(attributedString: toText)
finalString.append(emptyIconString)
finalString.append(iconString)
}else{
finalString = NSMutableAttributedString(attributedString: iconString)
finalString.append(emptyIconString)
finalString.append(toText)
}
return finalString
}