iOS - WebView 和字符串

发布于 2024-12-02 19:49:21 字数 108 浏览 4 评论 0 原文

我有一个名为 htmlString 的字符串,其中包含一些 html 格式的信息。我需要将这些信息放入加载整个 html 字符串(包含颜色和字体)的 webView 中。我需要知道弦的高度。我该怎么办?

i have a string called htmlString that contains some informations formatted in html. I need to put these info into a webView that load the entire html string, with color and fonts. And i need to know the string height. How can i do?

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

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

发布评论

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

评论(2

转角预定愛 2024-12-09 19:49:21

您想要执行以下操作:

[_webView loadHTMLString:htmlStr
                 baseURL:[NSURL fileURLWithPath:path]];

您可以在此处查看文档: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/Reference/Reference.html

这将加载您的HTML 插入 webview 并使用您提供的路径作为其他文档的根目录。换句话说,您的 html 字符串可以引用其他文件(css、javascript 等),并且 baseURL 用于定位使用相对路径的 url。

编辑:

要获取高度,您可以分配 UIWebView 的委托,因为它有一个 webViewDidFinishLoad: 方法来告诉您页面何时呈现。然后,您可以在页面上执行 javascript 来使用 UIWebView 的方法确定最终高度 - stringByEvaluatingJavaScriptFromString:

这个答案似乎也非常相关: 如何在可变高度 UITableView 内根据内容确定 UIWebView 高度?

You want to do something like:

[_webView loadHTMLString:htmlStr
                 baseURL:[NSURL fileURLWithPath:path]];

You can view the docs here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/Reference/Reference.html

This will load your HTML into the webview and use the path you provide as a root for other documents. In other words, your html string could reference other files (css, javascript, etc...) and the baseURL is used to locate the urls that use relative paths.

EDIT:

To get the height, you could assign the UIWebView's delegate as it has a webViewDidFinishLoad: method to tell you when the page is rendered. Then you could execute javascript on the page to determine the final height using UIWebView's method - stringByEvaluatingJavaScriptFromString:

This answer also seems pretty relevant: How to determine UIWebView height based on content, within a variable height UITableView?

静水深流 2024-12-09 19:49:21

NSString *htmlStrings=“

你好,我在这里”

";
[_webView loadHTMLString:htmlStrings
baseURL:[NSURL fileURLWithPath:null]];

NSString *htmlStrings="

Hello I m here

";
[_webView loadHTMLString:htmlStrings
baseURL:[NSURL fileURLWithPath:null]];

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