gpt4 book ai didi

ios - 粘贴格式化文本,而不是图像或 HTML

转载 作者:可可西里 更新时间:2023-11-01 05:33:56 24 4
gpt4 key购买 nike

我正在尝试模拟 iOS 页面和 Keynote 应用程序的粘贴板行为。简而言之,允许将基本的 NSAttributedString 文本格式(即 BIU)粘贴到 UITextView 中,而不是图像、HTML 等。

行为总结

  1. 如果您从 Notes 应用程序、Evernote 复制格式化文本,或者从网站复制文本和图像,Pages 只会粘贴纯文本字符串
  2. 如果您从 Pages 或 Keynote 中复制带格式的文本,它会将带格式的文本粘贴到 Pages、Keynote 等中的其他位置。
  3. 一个意外的后果,但可能需要承认的是,Notes 应用程序或 Evernote 都不会粘贴从 Pages 或 Keynote 复制的格式化文本。我推测应用程序之间的差异是使用 NSAttributedStrings 而不是 HTML?

这是如何实现的?在 Mac OS 上,您似乎可以 ask the pasteboard to return different types本身,让它同时提供富文本和字符串表示,并首选使用富文本。不幸的是,iOS 似乎不存在 readObjectsForClasses。也就是说,我可以通过日志看到 iOS 确实有一个 RTF 相关类型的粘贴板,thanks to this post .但是,我无法找到一种方法来请求粘贴板内容的 NSAttributedString 版本,以便我可以优先粘贴它。

背景

我有一个应用程序允许 UITextViews 中文本的基本 NSAttributedString 用户可编辑格式(即粗体、斜体、下划线)。用户想要从其他应用程序复制文本(例如 Safari 中的网页、Notes 应用程序中的文本),然后粘贴到我的应用程序中的 UITextView 中。允许粘贴板默认运行意味着我最终可能会得到我的应用程序不打算处理的背景颜色、图像、字体等。下面的示例显示了当粘贴到我的应用程序的 UITextView 中时具有背景颜色的复制文本的外观。

enter image description here

我可以通过子类化 UITextView 来克服 1

- (void)paste:(id)sender
{
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
NSString *string = pasteBoard.string;
NSLog(@"Pasteboard string: %@", string);
[self insertText:string];
}

意外的后果是,无法保留从我的应用程序中复制的文本格式。用户可能想要从我的应用程序中的一个 UITextView 复制文本,然后将其粘贴到我的应用程序中的另一个 UITextView。他们希望保留格式(即粗体、斜体、下划线)。

感谢您的见解和建议。

最佳答案

一些复制/粘贴的好东西和想法,给你:)

// Setup code in overridden UITextView.copy/paste
let pb = UIPasteboard.generalPasteboard()
let selectedRange = self.selectedRange
let selectedText = self.attributedText.attributedSubstringFromRange(selectedRange)

// UTI List
let utf8StringType = "public.utf8-plain-text"
let rtfdStringType = "com.apple.flat-rtfd"
let myType = "com.my-domain.my-type"
  • 覆盖 UITextView 副本:并使用您的自定义粘贴板类型pb.setValue(selectedText.string, forPasteboardType: myType)
  • 允许富文本复制(复制:):

    // Try custom copy
    do {
    // Convert attributedString to rtfd data
    let fullRange = NSRange(location: 0, length: selectedText.string.characters.count)
    let data:NSData? = try selectedText.dataFromRange(fullRange, documentAttributes: [NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType])
    if let data = data {

    // Set pasteboard values (rtfd and plain text fallback)
    pb.items = [[rtfdStringType: data], [utf8StringType: selectedText.string]]

    return
    }
    } catch { print("Couldn't copy") }

    // If custom copy not available;
    // Copy as usual
    super.copy(sender)
  • 允许富文本粘贴(in paste:):

    // Custom handling for rtfd type pasteboard data
    if let data = pb.dataForPasteboardType(rtfdStringType) {
    do {

    // Convert rtfd data to attributedString
    let attStr = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType], documentAttributes: nil)

    // Bonus: Possibly strip all unwanted attributes here.

    // Insert it into textview
    replaceSelection(attStr)

    return
    } catch {print("Couldn't convert pasted rtfd")}
    }
    // Default handling otherwise (plain-text)
    else { super.paste(sender) }
  • 比使用自定义粘贴板类型更好的是,将所有可能需要的标签列入白名单,循环遍历并在粘贴时删除所有其他标签。

  • (奖励:通过去除您添加的不必要的属性(如字体和 fg-color)来帮助其他应用程序的用户体验)
  • 另外值得注意的是,当粘贴板包含特定类型时,textView 可能不想允许粘贴,以解决此问题:

    // Allow all sort of paste (might want to use a white list to check pb.items agains here)
    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
    if action == #selector(UITextView.paste(_:)) {
    return true
    }
    return super.canPerformAction(action, withSender: sender)
    }
  • 此外,cut: 也可以很好地实现。基本上只是 copy:replaceSelection(emptyString)

  • 为了您的方便:

    // Helper to insert attributed text at current selection/cursor position
    func replaceSelection(attributedString: NSAttributedString) {
    var selectedRange = self.selectedRange

    let m = NSMutableAttributedString(attributedString: self.attributedText)
    m.replaceCharactersInRange(self.selectedRange, withAttributedString: attributedString)

    selectedRange.location += attributedString.string.characters.count
    selectedRange.length = 0

    self.attributedText = m
    self.selectedRange = selectedRange
    }

祝你好运!

引用资料: Uniform Type Identifiers Reference

关于ios - 粘贴格式化文本,而不是图像或 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27769595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com