简体   繁体   English

iPhone:如何在UITextView中显示来自UIWebView HTML文档的文本

[英]iPhone: How to Display Text from UIWebView HTML Document in a UITextView

I have an RSS feed that gets arranged in a UITableView which lets the user select a story that loads in a UIWebView. 我有一个RSS提要,该提要被安排在UITableView中,它使用户可以选择载入UIWebView的故事。 However, I'd like to stop using the UIWebView and just use a UITextView or UILabel. 但是,我想停止使用UIWebView,而只使用UITextView或UILabel。

This png is what I am trying to do (just display the various text aspects of a news story): 这个png是我想要做的(仅显示新闻报道的各种文本内容):

替代文字

I have tried using: 我试过使用:

NSString *myText = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];

and assigning the string to a UILabel but it doesn't work from where I am implementing it in webViewDidFinishLoad (--is that not the proper place?). 并将该字符串分配给UILabel,但是从我在webViewDidFinishLoad实现它的位置开始,它webViewDidFinishLoad (- webViewDidFinishLoad吗?)。 I get a blank textView and normal webView. 我得到一个空白的textView和普通的webView。

If I overlay a UITextView on top of a UIWebView on its own (that is, a webView that just loads one page), the code posted above works displays the text fine. 如果我将UITextView自身覆盖在UIWebView的顶部(即仅加载一页的webView)之上,则上面发布的代码可以正常显示文本。 The problem arises when I try to process the RSS feed . 当我尝试处理RSS feed时出现问题。

I've been stuck wondering why this doesn't work as it should for a few days now. 我一直在想为什么这几天不能正常工作了。 If you have a better, more efficient way of doing it then placing the code in webViewDidFinishLoad , please let me know! 如果您有更好,更有效的方法,请将代码放入webViewDidFinishLoad ,请告诉我! Does it go in my didSelectRowAtIndexPath? 它放在我的didSelectRowAtIndexPath?

Thank you very much in advance! 提前非常感谢您!

I think the you should first log the string returned by : 我认为您应该首先记录由以下命令返回的字符串:

NSString *myText = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];

... in the case of the RSS feed to make sure that you are getting something back. ...以RSS提要为例,以确保您能得到一些回报。 It's possible the RSS page doesn't have the same javascript components and that it returns an empty string. RSS页面可能没有相同的javascript组件,并且返回了一个空字符串。

Once you've confirmed that, then it becomes a simple matter of getting it to display properly in the text view. 确认后,将其简单地显示在文本视图中就变得很简单。

If the NSString you want to display is not empty, try to do something like this in the webViewDidFinishLoad method: 如果要显示的NSString不为空,请尝试在webViewDidFinishLoad方法中执行以下操作:

[yourUILabel performSelectorOnMainThread:@selector(setText:) withObject:[NSString stringWithFormat:@"bla bla %@", @"more bla"] waitUntilDone:YES];

The main thread of an iphone app is responsible for drawing components, that is why your label doesn't show your text. iPhone应用程序的主线程负责绘制组件,这就是为什么标签不显示文本的原因。

You could also try setting setNeedsDisplay: to true 您也可以尝试将setNeedsDisplay:设置为true

Also, the UILabel will not preserve the HTML format. 另外,UILabel将不会保留HTML格式。 It will display it as just text. 它将显示为纯文本。

You could try the following: 您可以尝试以下方法:

NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML;"];

NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML;"];

NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerText;"];

You lose out on formatting information with the last line of code. 您会失去最后一行代码的格式化信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM