1

我正在开发一个将图像与文本粘贴在一起的实用程序。到目前为止,我发现做到这一点的最好方法是NSPasteboard在 RTFD 数据流中使用和封装图像和文本。我遇到的问题是,即使我没有字体属性(我已经检查过),每次我将复制的代码粘贴到文档中时,它都会将字体更改为“Helvetica size 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)
}
4

0 回答 0