UI视图使用视口格式化格式

时间:2009-05-11 21:43:14

标签: iphone cocoa-touch uiwebview

我正在使用这里提到的元视口Why does UIWebView shrink images?用于UIWebView。这格式肖像很好,我不必补偿奇怪的缩放。但是,一旦旋转到横向,我就无法利用额外的宽度。意思是,我想把更多的文字放到一行中用于景观。一切都保持在1.0。有没有办法在旋转时将视口更改为.75缩放并在纵向上更改为1.0?

1 个答案:

答案 0 :(得分: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;
}