-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];

}

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.