NSPasteboard与RTF / RTFD更改字体

时间:2016-02-08 15:55:49

标签: cocoa nspasteboard

我正在使用粘贴图像的实用程序。到目前为止,我发现这样做的最好方法是使用NSPasteboard并将图像和文本封装在RTFD数据流中。我遇到的问题是,即使我没有任何字体属性(我已经检查过),每次将复制的代码粘贴到文档中时,它都会将字体更改为' Helvetica尺寸12'。是否可以强制粘贴文本和图像而不更改字体?

void copyData() {

    let pboard = NSPasteboard.generalPasteboard()
    pboard.clearContents()

    let str = NSMutableAttributedString()

    for im in images {
        let a = NSTextAttachment()
        a.image = im

        let s = NSAttributedString(attachment: a)
        str.appendAttributedString(s)
    }

    //now I have an rtf with a whole bunch of images
    let range = NSMakeRange(0, str.length)

    if let d = str.RTFDFromRange(range, documentAttributes: [NSDocumentTypeDocumentAttribute: NSPlainTextDocumentType]) {
        pboard.setData(d, forType: NSPasteboardTypeRTFD)
    }

    pboard.writeObjects(images)
}

0 个答案:

没有答案