如何将富文本粘贴到UITextView中? [英] How to paste rich text into a UITextView?

查看:105
本文介绍了如何将富文本粘贴到UITextView中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序允许用户通过单击一些将属性应用于文本视图的attributedText属性的格式设置按钮来格式化UITextView中的文本。我想允许用户从一个UITextView复制其格式的文本,然后使用标准粘贴板和标准剪切/复制/粘贴菜单将其粘贴到另一个文本中。

My app allows users to format text in a UITextView by clicking some formatting buttons that apply attributes to the attributedText property of the text view. I want to allow users to copy their formatted text from one UITextView and paste it into another using the standard pasteboard and standard cut/copy/paste menu.

当前,如果我复制从UITextView格式化文本并将其粘贴到Mail应用程序中的新消息中,将保留格式-因此,将自动进行格式化文本的复制。但是,如果我将格式化后的文本粘贴到应用程序中的另一个UITextView中,则只会显示纯文本。

Currently if I copy formatted text from a UITextView and paste it into a new message in the Mail app, the formatting is preserved -- so the copying of formatted text is happening automatically. But if I paste the formatted text into another UITextView in my app, only the plain text appears.

请按照 iOS文本编程指南,我能够覆盖UITextView的粘贴方法并拦截粘贴的内容:

By following the "Pasting the Selection" section in the Text Programming Guide for iOS, I was able to override the paste method of UITextView and intercept the pasted content:

- (void)paste:(id)sender {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    NSLog(@"types available: %@", [pasteboard pasteboardTypes]);
    for (NSString *type in [pasteboard pasteboardTypes]) {
        NSLog(@"type %@ (%@): %@", type, NSStringFromClass([[pasteboard valueForPasteboardType:type] class]), [pasteboard valueForPasteboardType:type]);
    }
}

这向我显示粘贴板包含以下内容格式:com.apple.rtfd,public.rtf,public.text和 Apple Web Archive粘贴板类型。文本的值为纯文本字符串,rtf的值为RTF字符串,两种Apple格式的值为NSData。

This shows me that the pasteboard contains content in the following formats: com.apple.rtfd, public.rtf, public.text and "Apple Web Archive pasteboard type". The value for the text is a plain text string, the value for the rtf is an RTF string and the values for the two Apple formats are NSData.

卡住了。如何将这些项目之一从粘贴板数据转换为为UITextView设置的属性字符串?

This is where I'm stuck. How can I translate one of these items from the pasteboard data to an attributed string to set for the UITextView?

或者更好的是,有没有一种方法可以将UITextView配置为

Or better yet, is there a way to configure the UITextView to accept formatted text automatically when pasting, in the same way that it supplies formatted text automatically when copying?

推荐答案

邓肯在上面问:粘贴时自动接受格式化的文本,就像复制时自动提供格式化的文本一样。您确定已将uitextview设置为处理富文本。我不知道该设置是什么,但是我再次检查了UITextView类的引用,并找到了 allowsEditingTextAttributes 属性。我以前没有使用过它,因为我提供了自己的格式设置按钮,并且不需要启用系统的粗体/斜体/下划线选项。但是,当我将UITextView的属性设置为YES时,它便开始自动接受格式化的文本。哇!

Duncan asked above, "Are you sure you have the uitextview set to handle rich text." I wasn't aware of a setting for that, but I checked the UITextView class reference again and found the allowsEditingTextAttributes property. I hadn't used that before because I was providing my own formatting buttons and didn't need to enable the system Bold/Italic/Underline options. But when I set that property to YES for my UITextView, then it started accepting formatted text automatically. Whew!

这篇关于如何将富文本粘贴到UITextView中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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