38

scalePageToFit:NO を使用せずに、UIWebview のフォント サイズを増減する方法。

4

11 に答える 11

73

A- と A+ の 2 つのボタンがあります。

@interface
NSUInteger textFontSize;

- (IBAction)changeTextFontSize:(id)sender
{
    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];
    [web stringByEvaluatingJavaScriptFromString:jsString];
    [jsString release];
}
于 2009-11-24T14:07:52.290 に答える
11
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *fontSize=@"143";
    NSString *jsString = [[NSString alloc]      initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",[fontSize intValue]];
    [myWebview stringByEvaluatingJavaScriptFromString:jsString];

}
于 2013-07-10T13:14:44.003 に答える
9

現在、UIWebView で DOM を直接操作する公開された方法や、フォント サイズなどを処理する便利なメソッドはありません。レーダーをファイリングすることをお勧めします。

そうは言っても。他の Web ページと同様に、CSS を変更することでフォント サイズを変更できます。それが不可能な場合 (コンテンツを制御できない場合)、小さな JavaScript 関数を記述して、ページ DOM の CSS プロパティを変更し、次のように呼び出して実行できます。

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;
于 2009-02-26T06:08:28.843 に答える
1

UIWebViewでフォントサイズと色を変更するための私のものは次のとおりです。

 NSString *myString=@"Hello World!";
 int textFontSize=2;
 NSString *myHTML=[NSString stringWithFormat: @"<html><body><script>var str = '%@'; document.write(str.fontsize(%d).fontcolor('green'));</script></body></html>",myString,textFontSize];    
 [myWebView loadHTMLString:myHTML baseURL:nil];
于 2013-08-25T22:42:03.090 に答える
1

この簡単な方法を試してください

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    int fontValue = 150;
    NSString *webviewFontSize = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",fontValue];
    [your_webview stringByEvaluatingJavaScriptFromString:webviewFontSize];
}
于 2014-05-22T12:32:34.530 に答える
0

メソッドで、フェッチされた html 文字列内のフォント サイズのオカレンスを必要なフォント サイズ (この例では 40) に変更することにより、フェッチされた HTML データのフォント サイズを変更します。

strHtml = [self htmlEntityDecode:strHtml];//parsing the html string


-(NSString *)htmlEntityDecode:(NSString *)string
{    
string = [string stringByReplacingOccurrencesOfString:@"14px;"   withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"15px;"   withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"16px;"   withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"17px;"   withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"18px;"   withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"19px;"   withString:@"40"];
string = [string stringByReplacingOccurrencesOfString:@"20px;"   withString:@"40"];

 return string;
}

したがって、html 文字列: span style='font-size: 20px; なります: span style='font-size: 40

注: gt;、apos; などの他のオカレンスを変更します。Webview にロードする目的の文字列を取得するためにも。

于 2017-01-27T06:26:17.030 に答える