我有非常简单的代码将NSTextAttachment添加到UITextView:
override func viewDidLoad() {
super.viewDidLoad()
//textView.textDragInteraction?.isEnabled = false
textView.becomeFirstResponder()
textView.isEditable = true
let image = UIImage(named: "dog")
let textAttachment = NSTextAttachment()
textAttachment.bounds = CGRect(x: 0, y: 0, width: (image?.size.width)!, height: (image?.size.height)!)
textAttachment.image = image
let attrString = NSAttributedString(attachment: textAttachment)
textView.textStorage.insert(attrString, at: 252)
}
正常工作,除非拖动图片附件时UITextView无响应,光标消失,无法将附件放到所需位置:
稍微修改代码:
textAttachment.contents = UIImagePNGRepresentation(image!)as Data?
附加图像不正确,但其余图像按预期工作,光标位置可见,附件在所需位置下降。
这是一个错误吗?我花了很多时间找到解决方案,但没有成功...我需要可拖动的图像附件......任何帮助? 感谢