iOS - WebView 和字符串
我有一个名为 htmlString 的字符串,其中包含一些 html 格式的信息。我需要将这些信息放入加载整个 html 字符串(包含颜色和字体)的 webView 中。我需要知道弦的高度。我该怎么办?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一个名为 htmlString 的字符串,其中包含一些 html 格式的信息。我需要将这些信息放入加载整个 html 字符串(包含颜色和字体)的 webView 中。我需要知道弦的高度。我该怎么办?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
您想要执行以下操作:
您可以在此处查看文档: 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:
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?
NSString *htmlStrings=“
你好,我在这里”
";
[_webView loadHTMLString:htmlStrings
baseURL:[NSURL fileURLWithPath:null]];
NSString *htmlStrings="
Hello I m here
";
[_webView loadHTMLString:htmlStrings
baseURL:[NSURL fileURLWithPath:null]];