2

我正在使用这里提到的元视口为什么 UIWebView 会缩小图像?对于 UIWebView。这可以很好地格式化肖像,我不必补偿奇怪的缩放比例。但是,一旦旋转到横向,我就无法利用额外的宽度。意思是,我想将更多文本放入单行横向。一切都保持在 1.0。有没有办法将视口更改为旋转时的 0.75 缩放并在纵向时更改为 1.0?

4

1 回答 1

0

我编写了这个函数来设置视口宽度,因为 UIWebView 被调整大小。您可以轻松地调整它以设置除宽度以外的视口属性。

-(NSString *) setViewportWidth:(CGFloat)inWidth {
    NSString *result = [_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"(function ( inWidth ) { "
        "var result = ''; "
        "var viewport = null; "
        "var content = 'width = ' + inWidth; "
        "var document_head = document.getElementsByTagName('head')[0]; "
        "var child = document_head.firstChild; "
        "while ( child ) { "
            "if ( null == viewport && child.nodeType == 1 && child.nodeName == 'META' && child.getAttribute( 'name' ) == 'viewport' ) { "
                "viewport = child; "
                "content = child.getAttribute( 'content' ); "
                "if ( content.search( /width\\s=\\s[^,]+/ ) < 0 ) { "
                    "content = 'width = ' + inWidth + ', ' + content; "
                "} else { "
                    "content = content.replace( /width\\s=\\s[^,]+/ , 'width = ' + inWidth ); "
                "} "
            "} "
            "child = child.nextSibling; "
        "} "
        "if ( null != content ) { "
            "child = document.createElement( 'meta' ); "
            "child.setAttribute( 'name' , 'viewport' ); "
            "child.setAttribute( 'content' , content ); "
            "if ( null == viewport ) { "
                "document_head.appendChild( child ); "
                "result = 'append viewport ' + content; "
            "} else { "
                "document_head.replaceChild( child , viewport ); "
                "result = 'replace viewport ' + content; "
            "} "
        "} "
        "return result; "
    "})( %d )" , (int)inWidth]];

    return result;
}
于 2011-07-05T20:47:37.910 回答