NSRTFPboardType 并粘贴到 gmail 浏览器 window?
NSRTFPboardType & paste into gmail browser window?
我有一个 NSTextView
,其中包含一些带有属性的文本(语法突出显示)。我正在尝试使用一个复制选项来保持语法突出显示,以便我可以将其粘贴到 gmail 文本中 window。目前,当我复制粘贴时突出显示不会出现,但是如果我直接从这个 Whosebug 页面复制以下部分:
- (void) copyAsRTF
{
NSPasteboard *pateboard = [NSPasteboard generalPasteboard];
NSData * rtfData = [[self textStorage] RTFFromRange: [self selectedRange]
documentAttributes: nil];
if (rtfData)
{
NSString * test = [[NSString alloc] initWithData: rtfData
encoding: NSUTF8StringEncoding];
[pateboard declareTypes: @[NSRTFPboardType]
owner: self];
[pateboard setData: rtfData
forType: NSRTFPboardType];
} // End of we had data
} // End of copyAsRTF
并将其粘贴到 gmail 中,它将以完整的语法突出显示粘贴,没问题。上面的代码是我用来生成 RTF 代码的,我可以确认它确实生成了正确的 RTF,因为我生成了一个 test
变量。
知道我做错了什么吗?据我了解,这应该有效。
(我应该注意,我已经在多个浏览器中尝试过 - Chrome、Safari 和 Firefox)。
根据@Sega-Zero 的评论,我想到了以下内容(有点草率,但确实有效)。对于我的特定需求,html 颜色和字体应该匹配属性字符串,因此我枚举它并创建一个 html 变量。这适用于 Safari、Chrome 和 Firefox。
#define PasteBoardTypeHTML @"public.html"
- (void) copyAsRTF
{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
// Get our RTF data
NSData * rtfData = [[self textStorage] RTFFromRange: [self selectedRange]
documentAttributes: nil];
// Get the HTML for our current data
NSData *htmlData = [[self htmlForRange: self.selectedRange] dataUsingEncoding: NSUTF8StringEncoding];
NSMutableArray * clipboardTypes = [NSMutableArray array];
if (nil != rtfData)
{
[clipboardTypes addObject: NSPasteboardTypeRTF];
} // End of we have rtf
if(nil != htmlData)
{
[clipboardTypes addObject: PasteBoardTypeHTML];
} // End of we have html
// Set our pasteboard types (types need to be declared before we call setData).
[pasteboard declareTypes: clipboardTypes.copy
owner: self];
if (rtfData)
{
[pasteboard setData: rtfData
forType: NSPasteboardTypeRTF];
} // End of we have rtf
if(htmlData)
{
[pasteboard setData: htmlData
forType: PasteBoardTypeHTML];
} // End of we have html
} // End of copyAsRTF
- (NSString *) htmlForRange: (NSRange) range
{
NSMutableString * htmlOutput = [NSMutableString stringWithString: @"<meta charset='utf-8'><pre>"];
[[self textStorage] enumerateAttributesInRange: range
options: 0
usingBlock:
^(NSDictionary * attributes, NSRange range, BOOL * stop)
{
NSString * actualCode = [[self textStorage].string substringWithRange: range];
actualCode = [self textToHtml: actualCode];
NSMutableString * currentHtml = [NSMutableString stringWithString: @"<span"];
NSColor * color = attributes[NSForegroundColorAttributeName];
NSFont * font = attributes[NSFontAttributeName];
NSMutableArray * fontStyles = [NSMutableArray array];
if(nil != color)
{
NSString * fontColorStyle =
[NSString stringWithFormat: @"color: %@;", [color hexadecimalValue]];
[fontStyles addObject: fontColorStyle];
} // End of we have a color
if(nil != font)
{
NSString * fontDetailsStyle =
[NSString stringWithFormat: @"font-family: %@;", font.familyName];
[fontStyles addObject: fontDetailsStyle];
} // End of we have a font
if(nil != fontStyles && fontStyles.count > 0)
{
[currentHtml appendFormat: @" style=\"%@\"", [fontStyles componentsJoinedByString: @" "]];
} // End of we have font styles
[currentHtml appendString: @">"];
[currentHtml appendString: actualCode];
[currentHtml appendString: @"</span>"];
// Add our section
[htmlOutput appendString: currentHtml];
}]; // End of attribute enumerations
// End of html output
[htmlOutput appendString: @"</pre></meta>"];
return htmlOutput.copy;
} // End of htmlForRange:
- (NSString*)textToHtml:(NSString*)htmlString
{
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@">" withString:@">"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"""" withString:@"""];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
return htmlString;
} // End of textToHtml:
正如在评论中发现的那样,为了在 chrome 中工作,您需要将文本复制为 html(public.html 粘贴板类型),或者可能两者都是 html 一个 rtf 类型。
我有一个 NSTextView
,其中包含一些带有属性的文本(语法突出显示)。我正在尝试使用一个复制选项来保持语法突出显示,以便我可以将其粘贴到 gmail 文本中 window。目前,当我复制粘贴时突出显示不会出现,但是如果我直接从这个 Whosebug 页面复制以下部分:
- (void) copyAsRTF
{
NSPasteboard *pateboard = [NSPasteboard generalPasteboard];
NSData * rtfData = [[self textStorage] RTFFromRange: [self selectedRange]
documentAttributes: nil];
if (rtfData)
{
NSString * test = [[NSString alloc] initWithData: rtfData
encoding: NSUTF8StringEncoding];
[pateboard declareTypes: @[NSRTFPboardType]
owner: self];
[pateboard setData: rtfData
forType: NSRTFPboardType];
} // End of we had data
} // End of copyAsRTF
并将其粘贴到 gmail 中,它将以完整的语法突出显示粘贴,没问题。上面的代码是我用来生成 RTF 代码的,我可以确认它确实生成了正确的 RTF,因为我生成了一个 test
变量。
知道我做错了什么吗?据我了解,这应该有效。
(我应该注意,我已经在多个浏览器中尝试过 - Chrome、Safari 和 Firefox)。
根据@Sega-Zero 的评论,我想到了以下内容(有点草率,但确实有效)。对于我的特定需求,html 颜色和字体应该匹配属性字符串,因此我枚举它并创建一个 html 变量。这适用于 Safari、Chrome 和 Firefox。
#define PasteBoardTypeHTML @"public.html"
- (void) copyAsRTF
{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
// Get our RTF data
NSData * rtfData = [[self textStorage] RTFFromRange: [self selectedRange]
documentAttributes: nil];
// Get the HTML for our current data
NSData *htmlData = [[self htmlForRange: self.selectedRange] dataUsingEncoding: NSUTF8StringEncoding];
NSMutableArray * clipboardTypes = [NSMutableArray array];
if (nil != rtfData)
{
[clipboardTypes addObject: NSPasteboardTypeRTF];
} // End of we have rtf
if(nil != htmlData)
{
[clipboardTypes addObject: PasteBoardTypeHTML];
} // End of we have html
// Set our pasteboard types (types need to be declared before we call setData).
[pasteboard declareTypes: clipboardTypes.copy
owner: self];
if (rtfData)
{
[pasteboard setData: rtfData
forType: NSPasteboardTypeRTF];
} // End of we have rtf
if(htmlData)
{
[pasteboard setData: htmlData
forType: PasteBoardTypeHTML];
} // End of we have html
} // End of copyAsRTF
- (NSString *) htmlForRange: (NSRange) range
{
NSMutableString * htmlOutput = [NSMutableString stringWithString: @"<meta charset='utf-8'><pre>"];
[[self textStorage] enumerateAttributesInRange: range
options: 0
usingBlock:
^(NSDictionary * attributes, NSRange range, BOOL * stop)
{
NSString * actualCode = [[self textStorage].string substringWithRange: range];
actualCode = [self textToHtml: actualCode];
NSMutableString * currentHtml = [NSMutableString stringWithString: @"<span"];
NSColor * color = attributes[NSForegroundColorAttributeName];
NSFont * font = attributes[NSFontAttributeName];
NSMutableArray * fontStyles = [NSMutableArray array];
if(nil != color)
{
NSString * fontColorStyle =
[NSString stringWithFormat: @"color: %@;", [color hexadecimalValue]];
[fontStyles addObject: fontColorStyle];
} // End of we have a color
if(nil != font)
{
NSString * fontDetailsStyle =
[NSString stringWithFormat: @"font-family: %@;", font.familyName];
[fontStyles addObject: fontDetailsStyle];
} // End of we have a font
if(nil != fontStyles && fontStyles.count > 0)
{
[currentHtml appendFormat: @" style=\"%@\"", [fontStyles componentsJoinedByString: @" "]];
} // End of we have font styles
[currentHtml appendString: @">"];
[currentHtml appendString: actualCode];
[currentHtml appendString: @"</span>"];
// Add our section
[htmlOutput appendString: currentHtml];
}]; // End of attribute enumerations
// End of html output
[htmlOutput appendString: @"</pre></meta>"];
return htmlOutput.copy;
} // End of htmlForRange:
- (NSString*)textToHtml:(NSString*)htmlString
{
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@">" withString:@">"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"""" withString:@"""];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
return htmlString;
} // End of textToHtml:
正如在评论中发现的那样,为了在 chrome 中工作,您需要将文本复制为 html(public.html 粘贴板类型),或者可能两者都是 html 一个 rtf 类型。