-1

按照此处的说明,我在 UIWebView 中使用 IBActions 的两个按钮上使用了下面的代码,这些按钮的decrease/increase文本字体大小。

当前单击任一按钮会导致字体大小减小到可能的最小大小,而不是从当前大小向上或向下递增。

但是,我一直无法弄清楚如何设置文本字体大小的初始值,这应该可以解决问题。

我试过添加textFontSize = 100;,但这只允许文本(-5 or +5)向上或向下调整一个步骤。这几天让我很恼火。非常感谢任何帮助。

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

}
4

1 回答 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.

于 2012-07-29T23:58:40.393 回答