【发布时间】:2009-05-11 21:43:14
【问题描述】:
我正在使用这里提到的元视口Why does UIWebView shrink images? 用于 UIWebView。这可以很好地格式化肖像,我不必补偿奇怪的缩放。但是,一旦旋转到横向,我就无法利用额外的宽度。意思是,我想将更多文本放入一行中以用于横向。一切都保持在 1.0。有没有办法将视口在旋转时更改为 0.75,在纵向时更改为 1.0?
【问题讨论】:
标签: iphone cocoa-touch uiwebview
我正在使用这里提到的元视口Why does UIWebView shrink images? 用于 UIWebView。这可以很好地格式化肖像,我不必补偿奇怪的缩放。但是,一旦旋转到横向,我就无法利用额外的宽度。意思是,我想将更多文本放入一行中以用于横向。一切都保持在 1.0。有没有办法将视口在旋转时更改为 0.75,在纵向时更改为 1.0?
【问题讨论】:
标签: iphone cocoa-touch uiwebview
我编写了这个函数来设置视口宽度,因为 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;
}
【讨论】: