-1

I've got the code below working with IBActions on two buttons that decrease/increase text font size in a UIWebView by following the instructions here.

Currently clicking either button causes the font size to decrease to the smallest possible size instead of incrementing up or down from the current size.

However, I have been unable to work out how to set an initial value for text font size which should fix the problem.

I've tried adding textFontSize = 100; but this only allows the text to be resized one step (-5 or +5) up or down. This has been irritating me for days. Any help much appreciated.

- (IBAction)changeTextFontSize:(id)sender;
//textFontSize = 100;
{

    switch ([sender tag]) {

        case 1: // A-

            textFontSize = (textFontSize > 50) ? textFontSize -5 : textFontSize;

            break;
        case 2: // A+

           textFontSize = (textFontSize < 160) ? textFontSize +5 : textFontSize;
            break;
    }

    NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",
                        textFontSize];
    [webView stringByEvaluatingJavaScriptFromString:jsString];
   [jsString release];

}
Community
  • 1
  • 1
gazmac
  • 23
  • 6

1 Answers1

1

There are a couple of problems here:

  1. You don't need a semicolon after your method declaration. Remove it.
  2. You need to declare textFontSize somewhere outside your method, so it doesn't just get reset to 100 every time the method is called. Presumably place it as an instance variable or in file scope.

If you make those changes, that code should work properly.

Alexis King
  • 43,109
  • 15
  • 131
  • 205