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?
-
What do you mean by wanting to know the string height? You want to know how large the page is after rendering the html? – Sam Aug 31 '11 at 15:34
2 Answers
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?
-
1
-
The question seemed two fold. 1.) How to load html string into UIWebView to load color, fonts, etc... 2.) How to get the "string height"? You are right that I only answered the first part, but I'm not yet sure what he's asking for the second. You can calculate a string's height given 1.) a font and 2.) a width. However, that doesn't make sense for an html string. You'd have to render it before you could determine the height. – Sam Aug 31 '11 at 15:37
-
Andrea, if this answer doesn't help you, I will remove it. I just thought this was at least a part of what you were looking for. Please expound on what you mean by "string height". I'm assuming you don't want the html tags factored into the string height. You could look at http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html which is what people typically use, but this won't work if you want the height of the rendered html page. – Sam Aug 31 '11 at 15:38
-
@James I ammended my answer. +1 for your comment, I only had a partial solution. – Sam Aug 31 '11 at 15:48
NSString *htmlStrings="
Hello I m here
"; [_webView loadHTMLString:htmlStrings baseURL:[NSURL fileURLWithPath:null]];
- 113
- 1
- 6