我试图找到一种方法来UIPasteBoard将HTML字符串复制到粘贴板中,并能够将其粘贴到iOS上的不同邮件客户端.
我尝试了StackOverflow的两个接受的答案:https://stackoverflow.com/a/6566850/1249958 和/sf/answers/1533839821/.这些解决方案适用于默认的iOS邮件客户端,但它们对Gmail没有任何影响.
我知道Gmail接受来自粘贴板的HTML输入,因为从Safari复制HTML并将其粘贴到Gmail应用中可以正常工作.
Cocoa的新手,我正在试图弄清楚如何将NSAttributedString复制到粘贴板.我查看了文档并且不确定我是否应该使用NSPasteboardItem.
这是我必须复制常规NSString:
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSArray *types = [NSArray arrayWithObjects:NSStringPboardType, nil];
[pb declareTypes:types owner:self];
[pb setString:@"asdfasdf" forType:NSStringPboardType];
Run Code Online (Sandbox Code Playgroud)
如何设置NSAttributedString?
谢谢
我有兴趣让我的用户将他们输入的文本复制到剪切和粘贴缓冲区中,但我想将其作为HTML来实现.
这样的事情甚至可能吗?或者我需要使用MIME格式?(我不知道.)
谢谢.
我正在尝试模拟iOS Pages和Keynote应用程序的粘贴板行为。简而言之,允许将基本NSAttributedString文本格式(即BIU)粘贴到UITextView中,但不能粘贴到图像,HTML等中。
行为摘要
这是如何完成的?在Mac OS上,您似乎可以要求粘贴板返回不同类型的自身,让其提供富文本格式和字符串表示形式,并首选使用富文本格式。不幸的是,iOS似乎不存在readObjectsForClasses。就是说,由于这篇文章,我可以通过日志看到iOS确实具有与RTF相关的粘贴板类型。但是,我找不到一种方法来请求粘贴板内容的NSAttributedString版本,以便可以优先进行粘贴。
背景
我有一个应用程序,允许UITextViews中的文本的基本NSAttributedString用户可编辑格式(即,粗体,斜体,下划线)。用户想要从其他应用程序复制文本(例如Safari中的网页,Notes应用程序中的文本),以粘贴到我的应用程序中的UITextView中。允许剪贴板默认运行,意味着我可能会得到应用程序不打算处理的背景颜色,图像,字体等。下面的示例显示了文本,当粘贴到我的应用程序的UITextView中时,具有背景色的复制文本看起来如何。

我可以通过继承UITextView来克服1
- (void)paste:(id)sender
{
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
NSString *string = pasteBoard.string;
NSLog(@"Pasteboard string: %@", string);
[self insertText:string];
}
Run Code Online (Sandbox Code Playgroud)
意外的结果是,失去了保留从我的应用程序中复制的文本格式的功能。用户可能想要从我的应用程序中的一个UITextView复制文本,然后将其粘贴到我的应用程序中的另一个UITextView。他们希望保留格式(即粗体,斜体,下划线)。
的见解和建议表示赞赏。
Swift 和 iOS 新手。我试图允许用户在我的应用程序中复制 nsattributedstring 并将其粘贴到 Mail、iMessage 或他们选择的任何应用程序中。
@IBAction func action(sender: UIButton!) {
let stringAttributes = [
NSFontAttributeName: UIFont.boldSystemFontOfSize(14.0),
NSBackgroundColorAttributeName: UIColor.redColor(),
]
let attributedString = NSMutableAttributedString(string: "Hello world!", attributes: stringAttributes)
do {
let documentAttributes = [NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType]
let rtfData = try attributedString.dataFromRange(NSMakeRange(0, attributedString.length), documentAttributes: documentAttributes)
if let rtfString = String(data: rtfData, encoding: NSUTF8StringEncoding) {
let pb = UIPasteboard.generalPasteboard()
return pb.string = rtfString
}
}
catch {
print("error creating RTF from Attributed String")
}
}
Run Code Online (Sandbox Code Playgroud)
粘贴后,将返回:
你好世界!{ NSBackgroundColor = "UIDeviceRGBColorSpace 1 …
此行仅复制文本本身而不粘贴字体,并粘贴它:
pasteBoard.string = MainController.customTextView.text
Run Code Online (Sandbox Code Playgroud)
我尝试查看人们对这个问题的类似答案,但是我看到的几乎每个答案都在目标C中,并且已经过时。通过查看其他答案喜欢这个,我认为我得到了拷贝工作,但是当我粘贴复制的文本,它不糊什么。这是我要复制的功能:
@objc func handleCopyButton() {
MainController.customTextView.resignFirstResponder() // dismiss keyboard
// Setup code in overridden UITextView.copy/paste
let selectedRange = MainController.customTextView.selectedRange
let selectedText = MainController.customTextView.attributedText.attributedSubstring(from: selectedRange)
// UTI List
let utf8StringType = "public.utf8-plain-text"
let rtfdStringType = "com.apple.flat-rtfd"
// Try custom copy
do {
// Convert attributedString to rtfd data
let fullRange = NSRange(location: 0, length: selectedText.string.count)
let data:NSData? = try selectedText.data(from: fullRange, documentAttributes: [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.rtfd]) as NSData
if let data = data {
// Set pasteboard …Run Code Online (Sandbox Code Playgroud) uipasteboard ×5
ios ×4
clipboard ×1
cocoa ×1
cocoa-touch ×1
copy-paste ×1
gmail ×1
ios7 ×1
pasteboard ×1
swift ×1
uikit ×1