NSUserDefault
NSUserDefault

Reputation: 1834

Resize content frame in webView without modifying UIWebView frame?

How the content frame (say for example text) in UIWebView can be modified?

Please check the below image which explains my requirement.

Attached image

In the above image, the box with yellow border is UIWebView frame and in red is its content(HTML text in webView).The black line(near yellow box) is where the scroll bar should come. So what I need is, only decrease the content width of UIWebView without changing webView's frame and setting the scroll bar at the right edge of the webView.

Is it possible to achieve?

Please share your ideas.

Thank you.

Upvotes: 0

Views: 1547

Answers (2)

TheTiger
TheTiger

Reputation: 13354

[webView setOpaque:NO];
[webView setBackgroundColor:[UIColor redColor]];
NSString *htmlString = @"<html>\
                        <body>\
                        <div style='width:33%; color:white; background-color:black; margin:0px auto; text-align:center;'>Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</div>\
                        </body>\
                        </html>\
";

[webView loadHTMLString:htmlString baseURL:nil];
  1. Black view is contentView or any div.
  2. Red View is your webView.
  3. margin:0px auto; property make that div centered.
  4. You can change contentView's width according to you. See width:33% in div tag.

And the result is:-

enter image description here

Upvotes: 1

user2398911
user2398911

Reputation: 94

The UIScrollView property is available for UIWebView starting in iOS 5. You can use that to resize your view by implementing the webViewDidFinishLoad: message in your delegate, like this:

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    CGRect newBounds = webView.bounds;
    newBounds.size.height = webView.scrollView.contentSize.height;
    webView.bounds = newBounds;
}

Or

[self.webView stringByEvaluatingJavaScriptFromString:
     [NSString stringWithFormat:
     @"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ",
      (int)self.webView.frame.size.width]];

Upvotes: 0

Related Questions