2012-03-21 3 views
1

UIWebView에 대해 "scalesPageToFit"속성을 YES로 설정했습니다. 점점 확대되지만 페이지가로드 될 때 내용이 꼬집음없이 읽을 수없는 작은 글꼴로 표시됩니다. 확대/축소 배율을 기본값으로 설정할 수 있습니까?UIWebView 확대/축소

+0

정확하게 원하는 것은 HTML 콘텐츠를로드 할 때 100 % 확대/축소가되어야합니다. 확대하기 위해 집기를 확대 할 때 확대해야합니다. – Bharathi

+0

게시 한 코드를 사용해 보셨습니까? 잠깐만 - 문제를 해결할 것입니다! – dom

+0

'scalesPageToFit = YES' 그것은 작동하지만 기본 컨텐츠 크기를 변경합니다. [내 답변을보십시오. 도움이 될 것입니다.] (http://stackoverflow.com/questions/7134576/enable-zooming-pinch-on-uiwebview/23971234 # 23971234) – iPatel

답변

3

당신이 표시되는 HTML을 통해 설정 제어가있는 경우

그냥 <head> 내부 <meta name="viewport" content="width=device-width;initial-scale=1.0">을 넣어 그런 다음있는 UIWebView 자체를로드하는 사이트 ...에 대한 자세한입니다.

그렇지 않은 경우 : 당신은있는 UIWebView에 자바 스크립트를 삽입하고 실행, 그래서 작은 코드를 사용하여 메타 태그를 삽입 할 수있는 쉬운 방법이 있습니다 :

NSString *result = [webView stringByEvaluatingJavaScriptFromString: 
    [NSString stringWithFormat:@"var result = '';" 
    "var viewport = null;" 
    "var content = null;" 
    "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;" 
    "  viewport.setAttribute('content' , 'width=device-width;initial-scale=1.0');" 
    "  result = 'fixed meta tag';" 
    " }" 
    " child = child.nextSibling;" 
    "}" 
    "if (null == viewport)" 
    "{" 
    " var meta = document.createElement('meta');" 
    " meta.setAttribute('name' , 'viewport');" 
    " meta.setAttribute('content' , 'width=device-width;initial-scale=1.0');" 
    " document_head.appendChild(meta);" 
    " result = 'added meta tag';" 
    "}" 
    ] 
]; 

참고 니펫 죽일 것입니다, HTML 코드의 작성자가 만든 뷰포트 설정 ... 그래서 이상한 결과가 발생할 수 있습니다.

+0

JS의 더미 대신'var viewport = document.querySelector ('head meta [name = viewport]')는 어떻습니까? – andytuba