I am using the UIWebview for displaying the content. But I got the text cutting issue in web view. UIWebview content size get changes dynamically based on the image size and content.But that Webview frame is constant,
How to avoid it? Please refer the attachment image.
THanks in advance
Try to use the delegate that indicates that the content is loaded completely then get the height by a javascript line of code, then adjust the frame of your uiwebview accordingly:
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *output = [webView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight;"];
[webView setFrame:CGRectMake(webView.frame.origin.x, webView.frame.origin.y, webView.frame.size.width, [output intValue])];
}
Enjoy :)
Related
I have a html string which looks like
<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p><img src=\"http://dreamatico.com/data_images/flowers/flowers-4.jpg\" width=70 height=100 />
I want to load the text of this html and also the images either into a UITextView or UIWebView. How would I do that?
Well, you can't load images into a text view. But UIWebView is pretty easy, assuming you have a reference to one in the storyboard, or have alloc-init'd one, you can simply do
UIWebView * webView;
[webView loadHTMLString: #"<html><b>My HTML!</b></html>" baseURL: nil];
I am trying to use UIWebView to load a html string from the web and there are lot of image src within it. The problem is the webview can load the string in the html string but the images just do not show up. Here is part of the html string, is there any problem with it?
NSString *htmlString = #"<strong>多年购置户外行装后,在她二舅的怂恿下,终于开始了第一次重装徒步。先来一张照片啊。表现我双脚走天下的决心。此次全程都是手机拍摄,终于感觉到了手机拍摄质量达不到要求的痛苦。同在蓝天下,我们的心情无比跳跃。</strong>
<div style = \"height:4px;\"> <div><img
src=\"http://210.22.129.138:801/link/api/public/media/54d8b2536580f1f2405e79f1\" style=\"height:367px; width:553px\" /> 走天下的双脚!<div style = \"height:4px;\"> <div><img
src=\"http://210.22.129.138:801/link/api/public/media/54d8b2536580f1f2405e79f3\" style=\"height:553px; width:553px\" /> ";
I use this to load the html string:
[self.webView loadHTMLString:htmlString baseURL:nil];
Try copy paste the URL on the device web browser. And check whether the browser is able to display the image. If the browser is not able to show that image then check the following
Check the correctness of the url.
Check Firewall settings. (There is a port number specified in the url, so make sure that port number is open in your firewall)
Check the IP is accessible.
Use base url :
[self.webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://210.22.129.138:801/"]];
I have to render html into web-view, In some cases it renders nothing on webview when
-invalid html received from server or html contains flash,.swf and other unsuported media types for iOS.
Webview's webViewDidFinishLoad is getting called in this case also, so I am unable to get this using webview's delegate methods.
To detect these cases I am taking points colors on webview diagonally and when I get alpha 1, it means html render successfully else its a blank.
I am using UIView+ColorOfPoint for getting color at point.
Is there any best way to achieve the same. Please help if you any any better solution.
Thanks
Try to implement following code.
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[webView loadHTMLString:[NSString stringWithFormat:#"%#", error] baseURL:nil];
if (error)
{
// implement your code.
}
}
I'm trying to make a WebView load a page from HTML code I have stored as an NSData. I get a blank page when I try to do this. Is there anything wrong with what I'm doing when I load the page? If not, I need to look elsewhere in my program.
if (essence.html){ //essence.html is an NSData
NSLog(#"Inserting HTML code into browser window: %#", [[NSString alloc] initWithData:essence.html encoding: NSUTF8StringEncoding]);
[webView.mainFrame loadData:essence.html MIMEType: #"text/html" textEncodingName: #"utf-8" baseURL:nil]; //webView is a WebView
}
I created the conditions so essence.html contains HTML code from the page http://kathleenmelian.com/test.html (which just says "hello"). The NSLog prints this when the above code runs:
Inserting HTML code into browser window: <html><head></head><body>hello</body></html>
So essence.html definitely contains valid code that a browser should be able to load.
You could use
[webView loadHTMLString:
[[NSString alloc] initWithData:essence.html encoding:NSUTF8StringEncoding]
baseURL:nil];
The other idea would be to replace "utf-8" with "UTF-8", which in some cases is known to make a difference (not sure about UIWebView).
Sorry to bother you guys. I fixed some other bug in my program's model, and that somehow fixed THIS problem as well. I don't know how. Crisis averted.
I'm putting app-generated content into a UIWebView, and trying to test that I'm doing it correctly. Here's the HTML string I'm putting into the web view:
#"<html><head></head><body><p>Come, let me clutch thee. I have thee not, and yet I see thee still. Art thou not, fatal vision, sensible to feeling as to sight?</p></body></html>"
And it gets into the web view thus:
[self.webView loadHTMLString: [self HTMLStringForSnippet: model.body] baseURL: nil];
where model.body contains just the <p/> element, and -HTMLStringForSnippet: wraps it into well-formed HTML. In my test, I rely on the answers to this question to retrieve the HTML content of the body via JavaScript:
- (void)testCorrectHTMLLoadedInWebView {
UIWebView *bodyView = controller.webView;
NSString *bodyHTML = [bodyView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
STAssertEqualObjects(bodyHTML, model.body, #"Model body should be used for the web view content");
}
While I can see by stepping through the code that the UIWebView is created correctly and is passed the correct HTML in -loadHTMLString:baseURL:, in the test bodyHTML is empty. So what do I have to do to see the actual content I expect and previously passed to the web view?
It takes a noticeable time for the UIWebView to render therefore the problem may be that you access the content before it is fully loaded.
Hook on UIWebViewDelegate's webViewDidFinishLoad: method to make sure the content is ready.
Update:
Maybe WebViewJavascriptBridge will bring some help.