简体   繁体   中英

How To Make UIWebView's Content Bigger

I'm making a little app, and there's a small UIWebView there. The problem is that I need to make the content on it look either smaller or bigger. For example, text (like this) should be bigger or smaller. Before asking this question, I tried this:

[web setScalesPageToFit:YES];
[web setFrame:size];

It doesn't have the behavior I want. Any ideas? Thank you for answering!

Web视图用于显示HTML,因此,如果您控制HTML或至少了解其基本结构,则可以使用CSS修改样式

It can be done something like this:

-(void)setWebViewScale:(NSUInteger)pageScale {
     _textFontSize = pageScale;
    NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",pageScale];
    [_webView stringByEvaluatingJavaScriptFromString:jsString];
    [jsString release];
}

And +/- scale will done by:

- (IBAction)changeTextFontSize:(id)sender
{
    switch ([sender tag]) {
        case 1: // A-
            _textFontSize = (_textFontSize > MIN_PAGE_SCALE) ? _textFontSize - STEP_PAGE_SCALE : _textFontSize;
            break;
        case 2: // A+
            _textFontSize = (_textFontSize < MAX_PAGE_SCALE) ? _textFontSize + STEP_PAGE_SCALE : _textFontSize;
            break;
    }

    [self setWebViewScale:_textFontSize];
}

Defines MAX_PAGE_SCALE and MIN_PAGE_SCALE restricts content to be too big and too small, and STEP_PAGE_SCALE determine step of scale change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM