TextView
✢ NSTextView with Rich Strings
Paste (Ctrl+V): paste item is image
After get keyDown event Ctrl+V, the unicode character \u{fffc} will be added into textStorage of TextView as mark.The contents of NSTextView are all stored in textStorage.
When content is string, stored in textStorage as string.
When content is image, stored in textStorage as attachment.If there are attachments(images) in textStorage, call
attributeRunsfunc to get them all.
✢ textStorage: write & read image
- Write
let textView = NSTextView()
let image = NSImage(contentsOfFile: path)
let att = NSTextAttachment()
att.attachmentCell = NSTextAttachmentCell(imageCell: image)
let attString = NSAttributedString(attachment: att)
textView.textStorage.append(attString)
- Read
let attRuns = textStorage.attributeRuns
for attRun in attRuns {
var effRange = NSRange()
let attrs = attRun.attributes(at: 0, effectiveRange: &effRange)
let att = attrs[NSAttachmentAttributeName] as NSTextAttachment
let attCell = att.attachmentCell as NSCell
let image = attCell.image
}