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

swift - 从NSPasteboard检索到的backgroundColor属性丢失-LMLPHP

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

swift - 从NSPasteboard检索到的backgroundColor属性丢失-LMLPHP

这是我的代码:

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时,得到以下信息:


  可选([ PasteboardType(_rawValue:NeXT富文本格式v1.0粘贴板类型),__ObjC.NSPasteboard.PasteboardType(_rawValue:public.utf16-external-plain-text),__ObjC.NSPasteboard.PasteboardType(_rawValue:CorePasteboardFlavorType 0x75743136C)。 PasteboardType(_rawValue:public.utf8-plain-text),__ObjC.NSPasteboard.PasteboardType(_rawValue:NSStringPboardType),__ObjC.NSPasteboard.PasteboardType(_rawValue:dyn.ah62d4rv4gkNn81y65yru),__ObjCCateboardType(Type)类型:__ObjCB .NSPasteboard.PasteboardType(_rawValue:com.apple.traditional-mac-plain-text),__ObjC.NSPasteboard.PasteboardType(_rawValue:CorePasteboardFlavorType 0x54455854),__ObjC.NSPasteb oard.PasteboardType(_rawValue:dyn.ah62d4rv4gk81g7d3ru),__ObjC.NSPasteboard.PasteboardType(_rawValue:CorePasteboardFlavorType 0x7374796C),__ObjC.NSPasteboard.PasteboardType(_rawValue:PublicN:aste.N) __ObjC.NSPasteboard.PasteboardType(_rawValue:public.utf16-纯文本),__ObjC.NSPasteboard.PasteboardType(_rawValue:CorePasteboardFlavorType 0x75747874),__ObjC.NSPasteboard.PasteboardType(_rawValue:com.adob.pdf _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.ah62d4qmxhk4d425t ry1g44pdsm11g55gsu1e24psrq0zg55zsmv0npneqz30g6xmsb4g86u),__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:Apple Web Archive粘贴板类型),__ObjC.NSPasteboard.PasteboardType(_rawValue:dyn.ah62d4qmxh4gb1fggg1gg1ggg1gg1gg1gg1b1f1gf1b1e0f1f1e0b4f1e0f1e4e1e1e1e4e0f1e1e0人:com.microsoft.DataObject)])

最佳答案

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

10-08 12:35