iPhone:如何在 UITextView 中显示 UIWebView HTML 文档中的文本
我有一个 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):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您应该首先记录 : ... 在 RSS 提要的情况下返回的字符串,
以确保您得到一些信息。 RSS 页面可能没有相同的 javascript 组件,并且返回空字符串。
一旦您确认了这一点,那么让它在文本视图中正确显示就变得很简单了。
I think the you should first log the string returned by :
... 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.
如果您要显示的 NSString 不为空,请尝试在 webViewDidFinishLoad 方法中执行以下操作:
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:
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 trueAlso, the UILabel will not preserve the HTML format. It will display it as just text.
您可以尝试以下操作:
您会丢失最后一行代码的格式化信息。
You could try the following:
You lose out on formatting information with the last line of code.