如何以编程方式从UITextView中复制格式化文本(例如斜体),以便当粘贴到另一个程序(例如邮件)中时,格式将被保留?我假设正确的方法是将NSAttributedString复制到黑板上。现在,我可以通过以下方式将常规字符串复制到黑板上:
NSString *noteTitleAndBody = [NSString stringWithFormat:@"%@\n%@", [document title], [document body]];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = noteTitleAndBody;我注意到,如果我使用标准文本选择复制菜单项从我的UITextView中选择并复制文本,它就会按需要工作。但是我需要通过我创建的按钮来访问这个。
我想也许我可以调用UIResponderStandardEditActions复制方法。使用以下内容,粘贴到邮件中确实保留了格式,但我的应用程序也因此崩溃。
NSMutableAttributedString *noteTitleAndBody = [[NSMutableAttributedString alloc] initWithString:[document title]];
[noteTitleAndBody appendAttributedString:[document body]];
[noteTitleAndBody copy:nil];请举例说明正确的做法,不胜感激。
PS -我知道有一些与NSAttributedString和pasteboard相关的线程,但它们似乎要么是可可、不要引用UIResponderStandardEditActions复制方法,要么早于iOS 6,其中许多UITextView属性都可用。
发布于 2012-10-18 17:26:40
以下内容将适用于textView当前的第一个响应程序:
[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];因为我的复制按钮只有在我的textView辞职firstResponder时才能访问,所以我不得不做更多的事情:
[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];由于下列帖子为我指明了正确的方向:
https://stackoverflow.com/questions/12789507
复制相似问题