从NSPasteboard

时间:2018-03-20 21:26:52

标签: swift macos nsattributedstring nspasteboard

当我从NSPasteboard检索NSAttributedString时,背景颜色会丢失。这就是我要复制的内容:

enter image description here

这就是返回的内容(我将其保存到rtf文件中):

enter image description here

这是我的代码:

func readBoard () {
var board = NSPasteboard.general
    var attr = board.readObjects(forClasses: [NSAttributedString.self])![0] as! NSAttributedString
print(attr)
    export(attributedText: attr)
}

func export(attributedText:NSAttributedString) {

    let file = "Output.rtf"

        do {

            let range = NSRange(location: 0, length: attributedText.length)

            if let data = attributedText.rtf(from: range, documentAttributes: [NSAttributedString.DocumentAttributeKey.characterEncoding : String.Encoding.utf8, NSAttributedString.DocumentAttributeKey.documentType : NSAttributedString.DocumentType.rtf]) {

                if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
                    let url = dir.appendingPathComponent(file)

                    try! data.write(to: url)
                }
            }

        }
        catch {

        }


    }

如何保留backgroundColor属性?感谢。

编辑:

当我打印board.types时,我明白了:

  

可选([__ ObjC.NSPasteboard.PasteboardType(_rawValue:dyn.ah62d4qmxhk4d425try1g44pdsm11g55gsu1e82xnqzv1kpneqz30g6xmsb4g86u),__ObjC.NSPasteboard.PasteboardType(_rawValue:com.microsoft.Object-描述符),__ObjC.NSPasteboard.PasteboardType(_rawValue:public.rtf),__ObjC .NSPasteboard.PasteboardType(_rawValue:NeXT Rich Text Format v1.0粘贴板类型),__ ObjC.NSPasteboard.PasteboardType(_rawValue:public.utf16-external-plain-text),__ ObjC.NSPasteboard.PasteboardType(_rawValue:CorePasteboardFlavorType 0x75743136),__ ObjC .NSPasteboard.PasteboardType(_rawValue:public.utf8-plain-text),__ ObjC.NSPasteboard.PasteboardType(_rawValue:NSStringPboardType),__ ObjC.NSPasteboard.PasteboardType(_rawValue:dyn.ah62d4rv4gk81n65yru),__ ObjC.NSPasteboard.PasteboardType(_rawValue:CorePasteboardFlavorType 0x7573746C ),__ ObjC.NSPasteboard.PasteboardType(_rawValue:com.apple.traditional-mac-plain-text),__ ObjC.NSPasteboard.PasteboardType(_rawValue:CorePasteboardFlavorType 0x54455854),__ ObjC.NSPas teboard.PasteboardType(_rawValue:dyn.ah62d4rv4gk81g7d3ru),__ObjC.NSPasteboard.PasteboardType(_rawValue:CorePasteboardFlavorType 0x7374796C),__ObjC.NSPasteboard.PasteboardType(_rawValue:public.html),__ObjC.NSPasteboard.PasteboardType(_rawValue:苹果HTML纸板型), __ObjC.NSPasteboard.PasteboardType(_rawValue:public.utf16-plain-text),__ ObjC.NSPasteboard.PasteboardType(_rawValue:CorePasteboardFlavorType 0x75747874),__ ObjC.NSPasteboard.PasteboardType(_rawValue:com.adobe.pdf),__ ObjC.NSPasteboard.PasteboardType( _rawValue:苹果PDF纸板型),__ObjC.NSPasteboard.PasteboardType(_rawValue:dyn.ah62d4qmxhk4d425try1g44pdsm11g55gsu1en5pcqzwc4y5tsz3gg3k),__ObjC.NSPasteboard.PasteboardType(_rawValue:com.microsoft.Embed-源),__ObjC.NSPasteboard.PasteboardType(_rawValue:dyn.ah62d4qmxhk4d425try1g44pdsm11g55gsu1e24psrq0zg55zsmv0n) ,__ ObjC.NSPasteboard.PasteboardType(_rawValue:com.microsoft.Link-Source),__ ObjC.NSPasteboard.PasteboardType(_rawValue:dyn.ah62d4qmxhk4d4 25try1g44pdsm11g55gsu1e24psrq0zg55zsmv0npneqz30g6xmsb4g86u),__ObjC.NSPasteboard.PasteboardType(_rawValue:com.microsoft.Link - 源描述符),__ObjC.NSPasteboard.PasteboardType(_rawValue:dyn.ah62d4qmxhk4d425try1g44pdsm11g55gsu1e82xnqzv1kxdmr3zu),__ObjC.NSPasteboard.PasteboardType(_rawValue:com.microsoft.ObjectLink), __ObjC.NSPasteboard.PasteboardType(_rawValue:com.apple.webarchive),__ObjC.NSPasteboard.PasteboardType(_rawValue:苹果Web档案纸板型),__ObjC.NSPasteboard.PasteboardType(_rawValue:dyn.ah62d4qmxhk4d425try1g44pdsm11g55gsu1ek2pyqfh0e4xfqr4a),__ObjC.NSPasteboard.PasteboardType(_rawValue :com.microsoft.DataObject)])

1 个答案:

答案 0 :(得分:0)

我发现了什么问题。当从文本编辑中复制突出显示的文本时,输出仅具有backgroundColor(可能是因为它是rtf文件)。我正在从Word复制突出显示。