html5 复制 ios,ios - Copy HTML to UIPasteboard with React Native - Stack Overflow

该博客介绍了如何在iOS应用中复制HTML内容并保留格式,以及如何将HTML内容粘贴到目的地并保持样式。通过使用WKWebView、UILabel和UITextView,实现了从网页加载HTML,将其复制到剪贴板,并在合适的情况下将其粘贴为富文本。重点在于处理HTML数据类型以确保格式在粘贴时得以保留。
摘要由CSDN通过智能技术生成

EDIT

This is the second attempt. Earlier I understood you wanted to copy and paste the HTML as text but after your comments and edits to the question I now understand that you have some text that you format using HTML and you want to paste that text while retaining the formatting.

More or less as shown below.

GBbZA.gif

This also illustrates how to wire it up in storyboard. Here is the code.

#import "ViewController.h"

#import

#import

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel * statusLabel;

@property (weak, nonatomic) IBOutlet WKWebView * webView;

@property (weak, nonatomic) IBOutlet UITextView * textView;

@end

@implementation ViewController

#pragma mark -

#pragma mark Actions

- (IBAction)reloadButtonAction:(id)sender {

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.webfx.com/blog/images/assets/cdn.sixrevisions.com/0435-01_html5_download_attribute_demo/samp/htmldoc.html"]]];

}

- (IBAction)copyButtonAction:(id)sender {

if ( self.webView.loading ) {

self.statusLabel.text = @"Loading";

} else {

self.statusLabel.text = @"Copying ...";

[self.webView evaluateJavaScript:@"document.documentElement.outerHTML.toString()"

completionHandler: ^ ( id content, NSError * error ) {

if ( error ) {

self.statusLabel.text = @"Error";

} else if ( [content isKindOfClass:NSString.class] ) {

[UIPasteboard.generalPasteboard setData:[( NSString * ) content dataUsingEncoding:NSUTF8StringEncoding]

forPasteboardType:( NSString * ) kUTTypeHTML];

self.statusLabel.text = @"Copied HTML";

} else {

self.statusLabel.text = @"Unknown type";

}

}];

}

}

- (IBAction)pasteButtonAction:(id)sender {

if ( [UIPasteboard.generalPasteboard containsPasteboardTypes:@[( NSString * ) kUTTypeHTML]] ) {

// Convert to attributed string

NSError * cvtErr;

NSData * data = [UIPasteboard.generalPasteboard dataForPasteboardType:( NSString * ) kUTTypeHTML];

NSAttributedString * attr = [[NSAttributedString alloc] initWithData:data

options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType }

documentAttributes:NULL

error: & cvtErr];

if ( cvtErr ) {

self.statusLabel.text = @"Convert error";

} else if ( attr ) {

self.statusLabel.text = @"Attributed";

self.textView.attributedText = attr;

} else {

self.statusLabel.text = @"Unable to decode";

}

} else {

NSString * content = UIPasteboard.generalPasteboard.string;

if ( content ) {

// Paste plain text

self.textView.text = content;

self.statusLabel.text = @"Pasted";

} else {

self.statusLabel.text = @"No string content";

}

}

}

@end

The code is very similar to before. The problem is that HTML = plain text so to copy HTML and retain the style or formatting also depends on the destination app into which you paste it. That destination app needs to handle the HTML correctly for what you want to achieve.

Anyhow, here are the changes from earlier.

On the copy side: the text is now copied as HTML and not as a string. There is very little difference there except that now, unlike before, the string is converted to data and the type is marked as kUTTypeHTML.

On the paste side: the real difference is here. If the pasteboard contains HTML then it is retrieved and formatted using an attributed string. It also works fine if you e.g. paste into Notes BTW.

This illustrates the problem you are dealing with here. I could just as well have used the code I used earlier and everything would work but in stead of formatted HTML I would get the unformatted / source HTML.

Here I assume that you are interested in fairly simple but formatted text. You did not mention text specifically and if you e.g. wanted to copy an HTML formatted table then my example is not adequate as I use a UITextView as destination.

For more complex HTML I'd use a WKWebView as destination as well and set its HTML string to the pasted in HTML, but again, this shows how the destination app also needs to cooperate in handling the HTML correctly.

Finally, looking at the updated answer and your code it is difficult to spot a difference, so I suspect your problem could be earlier with content itself in

RCT_EXPORT_METHOD(setString:(NSString *)content)

UPDATE

I also looked at the git repository you mention. There I found the following

RCT_EXPORT_METHOD(hasString:(RCTPromiseResolveBlock)resolve

reject:(__unused RCTPromiseRejectBlock)reject)

{

BOOL stringPresent = YES;

UIPasteboard *clipboard = [UIPasteboard generalPasteboard];

if (@available(iOS 10, *)) {

stringPresent = clipboard.hasStrings;

} else {

NSString* stringInPasteboard = clipboard.string;

stringPresent = [stringInPasteboard length] == 0;

}

resolve([NSNumber numberWithBool: stringPresent]);

}

It looks wrong, I think that one line should be

stringPresent = [stringInPasteboard length] != 0;

HTH

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值