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

发布于 2024-08-24 23:46:01 字数 809 浏览 3 评论 0原文

我有一个 RSS 提要,它排列在 UITableView 中,让用户选择加载到 UIWebView 中的故事。但是,我想停止使用 UIWebView 并只使用 UITextView 或 UILabel。

这个png就是我想要做的(只是显示新闻报道的各个文本方面):

替代文字

我尝试过使用:

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

并将字符串分配给 UILabel,但它在我在 webViewDidFinishLoad 中实现它的地方不起作用(——这不是正确的地方吗?)。我得到一个空白的 textView 和正常的 webView。

如果我将 UITextView 单独覆盖在 UIWebView 之上(即,仅加载一页的 webView),则上面发布的代码可以正常显示文本。当我尝试处理 RSS 提要时出现问题。

几天来我一直想知道为什么这不起作用。如果您有更好、更有效的方法,然后将代码放在 webViewDidFinishLoad 中,请告诉我!它会出现在我的 didSelectRowAtIndexPath 中吗?

非常感谢您!

I have an RSS feed that gets arranged in a UITableView which lets the user select a story that loads in a UIWebView. However, I'd like to stop using the UIWebView and just use a UITextView or UILabel.

This png is what I am trying to do (just display the various text aspects of a news story):

alt text

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?). I get a blank textView and normal 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. The problem arises when I try to process the 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! Does it go in my didSelectRowAtIndexPath?

Thank you very much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

西瑶 2024-08-31 23:46:01

我认为您应该首先记录 : ... 在 RSS 提要的情况下返回的字符串,

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

以确保您得到一些信息。 RSS 页面可能没有相同的 javascript 组件,并且返回空字符串。

一旦您确认了这一点,那么让它在文本视图中正确显示就变得很简单了。

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. It's possible the RSS page doesn't have the same javascript components and that it returns an empty string.

Once you've confirmed that, then it becomes a simple matter of getting it to display properly in the text view.

葵雨 2024-08-31 23:46:01

如果您要显示的 NSString 不为空,请尝试在 webViewDidFinishLoad 方法中执行以下操作:

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

iPhone 应用程序的主线程负责绘制组件,这就是您的标签不显示文本的原因。

您还可以尝试将 setNeedsDisplay: 设置为 true

另外,UILabel 不会保留 HTML 格式。它将仅将其显示为文本。

If the NSString you want to display is not empty, try to do something like this in the webViewDidFinishLoad method:

[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.

You could also try setting setNeedsDisplay: to true

Also, the UILabel will not preserve the HTML format. It will display it as just text.

日裸衫吸 2024-08-31 23:46:01

您可以尝试以下操作:

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

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

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

您会丢失最后一行代码的格式化信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文