NSTextView 复制粘贴 NSTextAttachment on mac os
NSTextView copy paste NSTextAttachment on mac os
我使用了 NSTextView,并插入了一个图像 NSTextAttachment。当我 select 复制它,然后粘贴它时,它会在文本视图中显示粘贴的图像,但我无法从 NSTextView.attributedString 中获取内容字符串。为什么?
我遇到了类似的问题。不确定它是否正是您想要的,但我希望能够复制并粘贴 NSTextAttachment 的字符串表示形式。
我最终在自定义 NSTextView 中覆盖了 func writeSelection(to pboard: NSPasteboard, type: String) -> Bool
class:
override func writeSelection(to pboard: NSPasteboard, type: String) -> Bool {
let selectedAttributedString = attributedString().attributedSubstring(from: selectedRange())
let selectedAttributedStringCopy = NSMutableAttributedString(attributedString: selectedAttributedString)
selectedAttributedStringCopy.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0,(selectedAttributedString.string.characters.count)), options: .reverse, using: {(_ value: Any?, _ range: NSRange, _ stop: UnsafeMutablePointer<ObjCBool>) -> Void in
if let textAttachment = value as? NSTextAttachment, let textAttachmentCell = textAttachment.attachmentCell as? YouCustomTextAttachmentCellClass {
var range2: NSRange = NSRange(location: 0,length: 0)
let attributes = selectedAttributedStringCopy.attributes(at: range.location, effectiveRange: &range2)
selectedAttributedStringCopy.replaceCharacters(in: range, with: NSMutableAttributedString(string: textAttachmentCell.yourStringRepresentation))
selectedAttributedStringCopy.addAttributes(attributes, range: range)
// selectedAttributedStringCopy.insert(attachmentAttributedString, at: range.location)
}
})
pboard.clearContents()
pboard.writeObjects([selectedAttributedStringCopy])
return true
}
请注意,我使用的是自定义 NSTextAttachmentCell class,它还记得它的字符串表示形式。
我使用了 NSTextView,并插入了一个图像 NSTextAttachment。当我 select 复制它,然后粘贴它时,它会在文本视图中显示粘贴的图像,但我无法从 NSTextView.attributedString 中获取内容字符串。为什么?
我遇到了类似的问题。不确定它是否正是您想要的,但我希望能够复制并粘贴 NSTextAttachment 的字符串表示形式。
我最终在自定义 NSTextView 中覆盖了 func writeSelection(to pboard: NSPasteboard, type: String) -> Bool
class:
override func writeSelection(to pboard: NSPasteboard, type: String) -> Bool {
let selectedAttributedString = attributedString().attributedSubstring(from: selectedRange())
let selectedAttributedStringCopy = NSMutableAttributedString(attributedString: selectedAttributedString)
selectedAttributedStringCopy.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0,(selectedAttributedString.string.characters.count)), options: .reverse, using: {(_ value: Any?, _ range: NSRange, _ stop: UnsafeMutablePointer<ObjCBool>) -> Void in
if let textAttachment = value as? NSTextAttachment, let textAttachmentCell = textAttachment.attachmentCell as? YouCustomTextAttachmentCellClass {
var range2: NSRange = NSRange(location: 0,length: 0)
let attributes = selectedAttributedStringCopy.attributes(at: range.location, effectiveRange: &range2)
selectedAttributedStringCopy.replaceCharacters(in: range, with: NSMutableAttributedString(string: textAttachmentCell.yourStringRepresentation))
selectedAttributedStringCopy.addAttributes(attributes, range: range)
// selectedAttributedStringCopy.insert(attachmentAttributedString, at: range.location)
}
})
pboard.clearContents()
pboard.writeObjects([selectedAttributedStringCopy])
return true
}
请注意,我使用的是自定义 NSTextAttachmentCell class,它还记得它的字符串表示形式。