将文本与格式化 - iOS 6 NSAttributedString复制到粘贴板 [英] Copy Text with Formatting - iOS 6 NSAttributedString to Pasteboard

查看:620
本文介绍了将文本与格式化 - iOS 6 NSAttributedString复制到粘贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式从UITextView复制格式化文本(例如斜体),以便在粘贴到其他程序(例如邮件)中时,格式化将被保留?我假设正确的方法是将NSAttributedString复制到粘贴板。现在,我能够通过以下方式将一个常规字符串复制到粘贴板:

How can I programmatically copy formatted text (e.g. italic) from a UITextView, so that when pasted into another program (e.g. Mail) the formatting will be retained? I'm assuming the correct approach is to copy an NSAttributedString to the pasteboard. Right now, I am able to copy a regular string to the pasteboard via the following:

NSString *noteTitleAndBody = [NSString stringWithFormat:@"%@\n%@", [document title], [document body]];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = noteTitleAndBody;


$ b $ p我注意到,如果我选择和复制文本从我的UITextView与标准文本选择复制菜单项,它根据需要工作。但我需要通过我创建的按钮访问。

I noticed that if I select and copy text from my UITextView with the standard text selection Copy menu item, it works as desired. But I need to access this via a button I've created.

我想也许我可以调用 UIResponderStandardEditActions 复制方法。使用以下内容,粘贴到Mail中保留了格式,但是我的应用程序也崩溃了。

I thought perhaps I could just call the UIResponderStandardEditActions Copy method. Using the following, the paste into Mail did retain the formatting, but my app also crashed as a result.

NSMutableAttributedString *noteTitleAndBody = [[NSMutableAttributedString alloc] initWithString:[document title]];
[noteTitleAndBody appendAttributedString:[document body]];
[noteTitleAndBody copy:nil];

这样做的正确方法的示例将非常感激。

Examples of the correct way to do this would be greatly appreciated.

PS - 我知道有与NSAttributedString和粘贴板相关的现有线程,但他们似乎要么 for Cocoa 不要引用UIResponderStandardEditActions复制方法

PS - I am aware that there are existing threads related to NSAttributedString and the pasteboard, but they seem to either be for Cocoa, don't reference the UIResponderStandardEditActions Copy method, or predate iOS 6 where many of the UITextView attributes became available.

推荐答案

以下内容适用于任何textView当前第一个responder:

The following will work for whatever textView is currently first responder:

[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];

由于我的复制按钮只有在我的textView退出firstResponder时才能访问,所以我不得不多做一些:

Since my copy button is only accessible when my textView had resigned firstResponder, I had to do a little more:

[self.noteTextView select:self];
self.noteTextView.selectedRange = NSMakeRange(0, [self.noteTextView.text length]);
[[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
[self.noteTextView resignFirstResponder];

感谢以下帖子指出我的方向:

Thanks to the following posts which pointed me in the right direction:

从UIResponderStandardEditActions执行复制/剪切

我可以调用select方法从UIResponderStandardEditActions?

这篇关于将文本与格式化 - iOS 6 NSAttributedString复制到粘贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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