1

属性付きのテキストを含むがNSTextViewあります (構文の強調表示)。Gmail のテキスト ウィンドウに貼り付けられるように、構文を強調表示したままにするコピー オプションを用意しようとしています。現在、コピーして貼り付けても強調表示は表示されませんが、このスタックオーバーフローページから次のセクションを直接コピーすると:

- (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 コードを生成するために使用するものであり、test変数が生成されているため、適切な RTF が生成されることを確認できます。

ここで私が間違っていることはありますか?私の理解では、これはうまくいくはずです。

(複数のブラウザー (Chrome、Safari、Firefox) で試したことに注意してください)。

4

2 に答える 2

2

コメントで発見されたように、クロムで作業するには、テキストを html (public.html ペーストボード タイプ) としてコピーするか、おそらく html と rtf タイプの両方をコピーする必要があります。

于 2015-06-17T17:38:11.453 に答える
1

@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:@"&amp;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<"  withString:@"&lt;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@">"  withString:@"&gt;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"""" withString:@"&quot;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"'"  withString:@"&#039;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
    return htmlString;
} // End of textToHtml:
于 2015-06-17T17:23:23.000 に答える