iOS: How to copy HTML into the cut-paste buffer?

前端 未结 4 721
春和景丽
春和景丽 2020-12-08 18:09

I\'m interested in letting my users copy the text they\'ve entered into the cut-and-paste buffer, but I\'d like to do that as HTML.

Is such a thing even possible? O

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 18:30

    This solution puts both a HTML and a plain text representation into the pasteboard:

    #import 
    
    NSString *html = @"

    Headline

    text"; NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *dict = @{@"WebMainResource": @{@"WebResourceData": data, @"WebResourceFrameName": @"", @"WebResourceMIMEType": @"text/html", @"WebResourceTextEncodingName": @"UTF-8", @"WebResourceURL": @"about:blank"}}; data = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListXMLFormat_v1_0 options:0 error:nil]; NSString *archive = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSString *plain = [html stringByReplacingOccurrencesOfRegex:@"<[^>]+>" withString:@""]; [UIPasteboard generalPasteboard].items = @[@{@"Apple Web Archive pasteboard type": archive, (id)kUTTypeUTF8PlainText: plain}];

    It uses -stringByReplacingOccurrencesOfRegex: from RegexKitLite to strip the HTML tags.

提交回复
热议问题