I am trying to copy an html data to clipboard on MAC. But when i check the clipboard using (Finder-> Edit Menu -> Show clipboard) it shows nothing. I want the formatted data to be pasted as it is. Below is the code i am using:
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSAttributedString *htmlString = [[NSAttributedString alloc]initWithString:@"<html><body><b>abcdefgh</b></body></html>"];
NSDictionary *documentAttributes = [NSDictionary dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute, nil];
NSData *htmlData = [htmlString dataFromRange:NSMakeRange(0, htmlString.length) documentAttributes:documentAttributes error:NULL];
[pb declareTypes:[NSArray arrayWithObject:NSHTMLPboardType] owner:nil];
[pb setData:htmlData forType:NSHTMLPboardType];
I will appreciate any help. Thanks!
Update:
To get the html formatted string in clipboard, i tried converting it to Attributed string and then from attributed string to rtf data, it sets the data to clip board perfectly, but when i try to paste the data in html editor ( http://htmleditor.in/ ) it loses some formatting like colour.
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSString *htmlString = @"<HTML> <BODY> <P><FONT COLOR=\"RED\"><UL><LI>fhhj</LI><LI>juil</LI></UL><B>hello</B> <i>html</i></FONT></P> </BODY></HTML>";
NSDictionary *documentAttributes = [NSDictionary dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute, NSCharacterEncodingDocumentAttribute,[NSNumber numberWithInt:NSUTF8StringEncoding], nil];
NSAttributedString* atr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:documentAttributes documentAttributes:nil error:nil];
NSData *rtf = [atr RTFFromRange:NSMakeRange(0, [atr length])
documentAttributes:nil];
[pb declareTypes:[NSArray arrayWithObject:NSRTFPboardType] owner:nil];
[pb setData:rtf forType:NSRTFPboardType];
How to preserve color when pasting to html editor? Text is shown with color on clipboard then why it isn't pasted with color on html editor (http://htmleditor.in/)?