问题
I have an NSAttributedString
which has an NSAttachment
and some text. I have copied it to the pasteboard. I have also made sure that the string has no font attributes. When I paste it, it always changes the font to Helvetica. Is there any way to prevent this behavior?
let wrapper = NSFileWrapper()
wrapper.preferredFilename = "image"
let attachment = NSTextAttachment(fileWrapper: wrapper)
if let im = NSImage(data: data) { attachment.image = im }
let image = NSAttributedString(attachment: attachment)
let str = NSMutableAttributedString(attributedString: image)
str.appendAttributedString(NSAttributedString(string: "hello world"))
let range = NSMakeRange(0, str.length)
//remove font attributes
str.removeAttribute(NSFontAttributeName, range: range)
str.removeAttribute(NSFontSizeAttribute, range: range)
Swift.print(str.attributes) //prints an empty array
At this point I copy the data to the clipboard
if let d = str.RTFDFromRange(range, documentAttributes: [NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType]) {
let pboard = NSPasteboard.generalPasteboard()
pboard.clearContents()
pboard.declareTypes([NSPasteboardTypeRTFD], owner: self)
pboard.setData(d, forType: NSPasteboardTypeRTFD)
}
来源:https://stackoverflow.com/questions/34915530/cocoa-nsattributedstring-pasting-changes-font