将格式为-iOS 6 NSAttributedString的文本复制到粘贴板
如何以编程方式从UITextView复制格式化文本(如斜体),以便在粘贴到其他程序(如邮件)时保留格式?我假设正确的方法是将NSAttribute字符串复制到粘贴板。现在,我可以通过以下方式将常规字符串复制到粘贴板:将格式为-iOS 6 NSAttributedString的文本复制到粘贴板,ios,copy,ios6,nsattributedstring,uipasteboard,Ios,Copy,Ios6,Nsattributedstring,Uipasteboard,如何以编程方式从UITextView复制格式化文本(如斜体),以便在粘贴到其他程序(如邮件)时保留格式?我假设正确的方法是将NSAttribute字符串复制到粘贴板。现在,我可以通过以下方式将常规字符串复制到粘贴板: NSString *noteTitleAndBody = [NSString stringWithFormat:@"%@\n%@", [document title], [document body]]; UIPasteboard *pasteboard = [UIPasteboa
NSString *noteTitleAndBody = [NSString stringWithFormat:@"%@\n%@", [document title], [document body]];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = noteTitleAndBody;
我注意到,如果我使用标准文本选择复制菜单项从UITextView中选择并复制文本,它将按需要工作。但我需要通过我创建的按钮来访问它
我想也许我可以调用Copy方法。使用下面的方法,粘贴到邮件中确实保留了格式,但我的应用程序也因此崩溃
NSMutableAttributedString *noteTitleAndBody = [[NSMutableAttributedString alloc] initWithString:[document title]];
[noteTitleAndBody appendAttributedString:[document body]];
[noteTitleAndBody copy:nil];
请举例说明正确的方法
PS-我知道存在与NSAttributedString和pasteboard相关的线程,但它们似乎要么存在,要么早于许多UITextView属性可用的iOS 6。以下内容适用于任何textView当前的第一响应程序:
[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
由于我的“复制”按钮只有在我的textView辞去第一响应者职务时才可访问,因此我必须做更多的工作:
[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];
感谢以下帖子为我指明了正确的方向:
您不必让
UITextView
成为firstResponder
,您只需调用sendAction:@selector(copy:)to:self.noteTextView
,谢谢您的建议。只要我继续为视图进行范围选择,这是可行的:self.noteTextView.selectedRange=NSMakeRange(0,[self.noteTextView.text length]);