0

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!

jrtc27
  • 8,496
  • 3
  • 36
  • 68
Adri
  • 243
  • 3
  • 12

2 Answers2

0

web views are used to display HTML, so if you control the HTML, or at least know its basic structure, you can use CSS to modify the style

wattson12
  • 11,176
  • 2
  • 32
  • 34
  • OK! I don't know anything about CSS, but I understand that if I add it in any soucre-code of any web page, I would be able to control the size. Any ideas for the CSS code? – Adri Feb 03 '12 at 13:46
  • i dont know css so usually just trial and error, but i dont think this would work if you are allowing someone to view any site, really only works for controlled or known HTML – wattson12 Feb 03 '12 at 14:02
0

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.

Gusev Andrey
  • 446
  • 5
  • 23