3

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.

4

3 回答 3

2

垂直居中:

CGSize contentSize = webView.scrollView.contentSize;
CGFloat d = contentSize.height/2 - webView.center.y;
[webView.scrollView setContentOffset:CGPointMake(0, d) animated:NO];
于 2014-03-06T23:09:31.297 回答
2

将所有内容保存在 a 中table,然后对齐表格单元格。

CSS

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

或者

<table border="1">
    <tr>
        <td align="center" valign="middle">Text</td>
        <td align="center" valign="middle">Text</td>
    </tr>
</table>
于 2013-05-24T22:54:43.093 回答
1

像这样试试

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];

希望对你有帮助......

于 2013-05-25T04:45:01.700 回答