UIWebView won't resize to fit content? - iphone

My webView is not resizing properly after it finished loading. I have tried this code.
- (void)webViewDidFinishLoad:(UIWebView *)webview {
CGRect frame = webView.frame;
frame.size.height = 1;
frame.size.width = screenWidth;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
[bodyScroll setContentSize:CGSizeMake(screenWidth, webView.frame.origin.y + webView.frame.size.height+keyboardRectY+60)];
It is incresing the width beyond the screenWidth and hence my content horizontally expands beyond the view size which I don't want.
changing
CGSize fittingSize = [webView sizeThatFits:CGSizeMake(screenWidht, 1)];
also leads to the same situation.
How can I get rid of this? The webview width should always remain as screenwidth and only it's height should adjust based on the content size so that the webView just about fits the whole content.
Thanks in Advance

When setting the size of frame, you are overwriting the width you set previously. This should it:
...
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
fittingSize.width = screenWidth; // Add this
frame.size = fittingSize
...

Related

Dynamic height of UIWebView with auto layout

I have a problem with setting the height of an UIWebView dynamically. The webview loads a product description which contains html. I want the UIWebView to change its height based on the contents of the description. The parent scrollview should also change its height, but then based on the height of the UIWebView.
Can anyone explain to me how I can achieve the desired behavior of my views?
This is my view hierarchy:
You have to add some code in the delegate method of UIWebView named webViewDidFinishLoad. Here is what you can do:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
yourScrollView.contentSize = webView.bounds.size;
}
The same thing can also be achieved using javascript to find the right height which is as follow:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGRect oldBounds = [[self webView] bounds];
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
NSLog(#"NEW HEIGHT %f", height);
[webView setBounds:CGRectMake(oldBounds.origin.x, oldBounds.origin.y, oldBounds.size.width, height)];
yourScrollView.contentSize = webView.bounds.size;
}
Hope it helps!
I actually had the same problem recently and ended up figuring it out and putting together a sample project on github.
You can set a constant height constraint on your WebView and change that dynamically once you calculate the height from the response.
Also constraint between a child view and a parent view can be achieved by this method in NSLayoutConstraint
+ (id)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c

UITextView will not resize

I have tried resizing a UITextView (inside a tableViewCell) to the content size. but it will not change its height at all. I have even changed the height of the UITableViewCell. What could be wrong?
- (void) setTextViewContents: (NSString*) string
{
[textView setText:string];
CGRect frame2 = self.frame;
frame2.size.height = 10000;
self.frame = frame2;
/* resize the view */
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height+60;
textView.frame = frame;
The string does appear on the view but the size does not change.
Try calling the -[UIView sizeToFit] method.

UIScrollView height to adjust when width is adjusted

I have a UIScrollView and I wanted the frame height to adjust proportionally when I adjust the width, is this possible? Basically I am talking about auto adjusting the frame height of the UIScrollView when I adjust the width of the UIScrollView? I have tried setting the autoResizingMask to UIViewAutoresizingFlexibleWidth and height, but this doesn't work
I don't think there's an automatic way to do it. but you can added a UIView+SCaleAddition to do this:
#interface UIView(RespectScale)
- (void)setWidthRespect:(float)w;
- (void)setHeightRespect:(float)h;
#end
#implement UIView(RespectScale)
- (void)setWidthRespect:(float)w
{
float scale = self.bounds.size.width / self.bounds.size.height;
float h = w/scale;
CGRect bounds = self.bounds
bounds.size.width = w;
bounds.size.height = h;
self.bounds = bounds;
}
- (void)setHeightRespect:(float)h
{
// write it your own
}
#end
You can do:
//get old proportions
CGFloat proportion = scrollView.frame.size.width / scrollView.frame.size.height;
CGRect frame = scrollView.frame;
frame.size.width = new_width;
frame.size.height = new_width * proportion;
scrollView.frame = frame;

UIWebView problem with size adjustment when rotating device

I have a UIWebview in which a have a local html placed . When the ipad shifts, i rearange the elements so that the UIWebView is smaller/bigger after the case. The problem is that although when i sift and adjust the width, although the width is adjusted, the text isn't . The text is visible only in the webview's frame but it goes offscreen (some text is hidden, it continues to some point not being visible) . How can i adjust the webview content width ?
I have faced this problem and what i did to solve is like this ... create a mothod
-(void)reconfigureFrame
{
[_yourWebView loadHTMLString:[NSString stringWithFormat:#"<html><body style='background-color: transparent; width: 280px; margin: 0; padding: 0;color:black;'><div id='ContentDiv'>%#</div></body></html>",theStringToShow] baseURL:nil];
}
in webView:DidLoad delegate method do this
NSString *contentHeight = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('ContentDiv').offsetHeight"];
CGRect frame = [containtView frame];
//containtView holds webView
frame.size.height = [contentHeight floatValue] + 10;
[containtView setFrame:frame];
frame = [webView frame];
frame.origin.x = 10;
frame.origin.y = 5;
frame.size.width = containtView.frame.size.width - 2*frame.origin.x;
frame.size.height = [contentHeight floatValue];
[webView setFrame:frame];
and in didAutoRotate just call the first method

How do I keep the text sharp while zooming when using a CATiledLayer to render a PDF

I have implemented a PDF viewer in my app. It's a scroll view which uses CATiledLayer for zooming.
A simpler approach would have been to use QLPreviewController, which supports PDF viewing with page control and zooming but I need fine control over the PDF page.
I have the viewer working but would like the redraw to kick in sooner. At the moment it only redraws the text when I zoom in to double the size (200%). So the text appears blurry until you zoom in far enough.
I have tried various combinations of levelOfDetail and levelOfDetailBias to try and get the re-drawing to occur sooner but to no avail.
Here is the code which creates the CATiledLayers:
//get the pdf crop box rectangle
CGPDFPageRef pageRef=[self.pdf getPdfPage:self.myPageNo];
CGRect cropBox = CGRectIntegral(CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox));
//scale to size of the screen
CGRect targetRect = self.view.bounds;
CGFloat xScale = targetRect.size.width / cropBox.size.width;
CGFloat yScale = targetRect.size.height / cropBox.size.height;
self.scale = xScale < yScale ? xScale : yScale;
//alway fill height of screen
self.scale = yScale;
//used to center the pdf horizontally
self.xOffSet = (targetRect.size.width - cropBox.size.width*self.scale)/2;
[self renderPDFPageToImage];
CGRect boundsRect = self.view.bounds;
//use a tiled layer for better performance
CATiledLayer *tiledLayer = [CATiledLayer layer];
tiledLayer.delegate = self;
tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
tiledLayer.levelsOfDetail = 4;
tiledLayer.levelsOfDetailBias = 4;
tiledLayer.frame = boundsRect;
self.myContentView = [[[UIView alloc] initWithFrame:boundsRect] autorelease];
[self.myContentView.layer addSublayer:tiledLayer];
CGRect viewFrame = self.view.frame;
viewFrame.origin = CGPointZero;
UIScrollView *_scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
_scrollView.delegate = self;
_scrollView.contentSize = boundsRect.size;
_scrollView.maximumZoomScale = 4;
[_scrollView addSubview:myContentView];
//retain it
self.scrollView=_scrollView;
[self.view addSubview:_scrollView];
[_scrollView release];
Please point me in the right direction.