Cocoa NSAttributedString Pasting Changes Font

谁说我不能喝 提交于 2019-12-24 15:42:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!