如何以编程方式复制格式化文本(例如italic)来自 UITextView,这样当粘贴到另一个程序(例如邮件)时,格式将被保留?我假设正确的方法是将 NSAttributedString 复制到粘贴板。现在,我可以通过以下方式将常规字符串复制到粘贴板:
NSString *noteTitleAndBody = [NSString stringWithFormat:@"%@\n%@", [document title], [document body]];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = noteTitleAndBody;
我注意到,如果我使用标准文本选择“复制”菜单项从 UITextView 中选择并复制文本,它会按预期工作。但我需要通过我创建的按钮访问它。
我想也许我可以打电话给UIResponder标准编辑操作复制方法。使用以下内容,粘贴到邮件中确实保留了格式,但我的应用程序也因此崩溃了。
NSMutableAttributedString *noteTitleAndBody = [[NSMutableAttributedString alloc] initWithString:[document title]];
[noteTitleAndBody appendAttributedString:[document body]];
[noteTitleAndBody copy:nil];
正确方法的示例将不胜感激。
PS - 我知道存在与 NSAttributedString 和粘贴板相关的现有线程,但它们似乎是对于可可, 不要引用 UIResponderStandardEditActions Copy 方法,或者早于 iOS 6,其中许多 UITextView 属性变得可用。
以下内容适用于当前第一响应者的任何 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];
感谢以下帖子为我指明了正确的方向:
从 UIResponderStandardEditActions 执行复制/剪切
我可以从 UIResponderStandardEditActions 调用 select 方法吗?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)