粘贴格式化文本,而不是图像或HTML [英] Paste Formatted Text, Not Images or HTML

查看:144
本文介绍了粘贴格式化文本,而不是图像或HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to emulate the pasteboard behavior of the iOS Pages and Keynote apps. In short, allowing basic NSAttributedString text formatting (i.e. BIU) to be pasted into a UITextView, but not images, HTML, etc.

行为摘要


  1. 如果您从Notes应用程序,Evernote或网站上的文本和图像复制格式化文本,则Pages将仅粘贴纯文本字符串

  2. 如果您从Pages或Keynote中复制格式化文本,它会将格式化文本粘贴到Pages,Keynote等中。

  3. 不良后果,但可能很重要承认,Notes应用程序或Evernote都不会粘贴从Pages或Keynote复制的格式化文本。我猜测应用程序之间的差异是使用NSAttributedStrings而不是HTML?

这是如何实现的?在Mac OS上,您似乎可以要求粘贴板返回自身的不同类型,让它同时提供富文本和字符串表示,并使用富文本作为首选。不幸的是,对于iOS,似乎不存在readObjectsForClasses。也就是说,我可以通过日志看到iOS确实有RTF相关类型的粘贴板,感谢这篇文章。但是,我无法找到一种方法来请求NSAttributedString版本的粘贴板内容,因此我可以优先考虑粘贴。

How is this accomplished? On Mac OS, it appears you can ask the pasteboard to return different types of itself, have it provide both a rich text and a string representation, and use rich text as preferred. Unfortunately, the readObjectsForClasses doesn't appear to exist for iOS. That said, I can see via log that iOS does have an RTF related type of pasteboard, thanks to this post. I can't however, find a way to request an NSAttributedString version of pasteboard contents so I can prioritize it for pasting.

背景

我有一个应用程序,它允许UITextViews中基本的NSAttributedString用户可编辑格式(即粗体,斜体,下划线)。用户希望从其他应用程序(例如Safari中的网页,Notes应用程序中的文本)复制文本,以粘贴到我的应用程序中的UITextView中。允许粘贴板作为默认操作意味着我可能最终得到我的应用程序不打算处理的背景颜色,图像,字体等。下面的示例显示了粘贴到我的应用程序的UITextView时带有背景颜色的复制文本的文本。

I have an app that allows basic NSAttributedString user editable formatting (i.e. bold, italic, underline) of text in UITextViews. Users want to copy text from other apps (e.g. web page in Safari, text in Notes app), to paste into a UITextView in my app. Allowing pasteboard to operate as default means I may end up with background colors, images, fonts, etc. that my app isn't intended to handle. Example below shows text how copied text with a background color looks when pasted into my app's UITextView.

我可以通过继承UITextView来克服1

I can overcome 1 by subclassing UITextView

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

意想不到的后果是,失去了保留从中复制的文本格式的能力在我的应用程序内用户可能希望从我的应用程序中的一个UITextView复制文本,并将其粘贴到我的应用程序中的另一个UITextView。他们希望保留格式(即粗体,斜体,下划线)。

The unintended consequence is, losing the ability to retain formatting of text that's copied from within my app. Users may want to copy text from one UITextView in my app, and paste it to another UITextView in my app. They will expect formatting (i.e. bold, italics, underline) to be retained.

洞察和建议表示赞赏。

推荐答案

这应该是对Leonard Pauli的答案的评论,但我还没有足够的声誉来发表评论。

This should be a comment on Leonard Pauli's answer, but I don't have enough reputation to make comments yet.

而不是:

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

(或者更新版Swift中的referencedString.string.count)

(or attributedString.string.count as it is in more recent versions of Swift)

最好使用:

selectedRange.location += attributedString.length

否则,当您粘贴包含导致referencedString.length和attributedString.string.count的表情符号的文本时如果不同,选择将最终出现在错误的位置。

Otherwise when you paste text that contains emoji that cause attributedString.length and attributedString.string.count to differ, the selection will end up in the wrong place.

这篇关于粘贴格式化文本,而不是图像或HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆