이 함수는 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;
}