Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center the contents of a webview in iOS vertically and horizontally?

I have googled quite a bit and I can't seem to find anything that makes sense to me. I need to center the contents of a webview both vertically and horizontally. I have been able to get the horizontal to work with this:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *bodyStyle = @"document.getElementsByTagName('body')[0].style.textAlign = 'center';";
    [self.webView stringByEvaluatingJavaScriptFromString:bodyStyle];
}

But I can't find anything on vertical. Any ideas? Any help would be appreciated.

like image 381
user2282932 Avatar asked Oct 17 '25 18:10

user2282932


2 Answers

Try like this,

NSString *bodyStyleVertical = @"document.getElementsByTagName('body')[0].style.verticalAlign = 'middle';";
NSString *bodyStyleHorizontal = @"document.getElementsByTagName('body')[0].style.textAlign = 'center';";
NSString *mapStyle = @"document.getElementById('mapid').style.margin = 'auto';";

[webView stringByEvaluatingJavaScriptFromString:bodyStyleVertical];
[webView stringByEvaluatingJavaScriptFromString:bodyStyleHorizontal];
[webView stringByEvaluatingJavaScriptFromString:mapStyle];

Hope it will helps you.....

like image 83
Venk Avatar answered Oct 20 '25 08:10

Venk


Keep everything in a table and then align the table cells.

CSS

td {
    text-align: center;
    vertical-align: middle;
}

or

<table border="1">
    <tr>
        <td align="center" valign="middle">Text</td>
        <td align="center" valign="middle">Text</td>
    </tr>
</table>
like image 45
karim Avatar answered Oct 20 '25 09:10

karim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!