我正在开发类似于ibook应用程序的iphone应用程序。我在解析XML中的epub文件后加载得到的UIWebView文件的内容。我更改了UIWebView内容的字体大小和字体系列。 ePub书中所有页面的字体大小和字体系列。如何存储UIWebView的设置,以便将其应用于EPub书中的所有页面。
这是我如何更改weview字体的代码。

NSString *yourHTMLSourceCodeString = [App_delegate.webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];

[App_delegate.webView loadHTMLString:[NSString stringWithFormat:@"<div style=' font-family:Times New Roman;'>%@",yourHTMLSourceCodeString] baseURL:nil];

最佳答案

检查此项目https://github.com/fedefrappi/AePubReader。您可以简单地将fontName和fontSize值存储在NSUserDefaults中,并在webView委托方法webViewDidFinishLoad中添加CSS规则:

- (void)webViewDidFinishLoad:(UIWebView *)theWebView{

    NSString *fontName = [[NSUserDefault standartUserDefaults] objectForKey:@"fontName"];
    NSNumber *currentTextSize = [[NSUserDefault standartUserDefaults] objectForKey:@"currentTextSize"];

    NSString *varMySheet = @"var mySheet = document.styleSheets[0];";

    NSString *addCSSRule =  @"function addCSSRule(selector, newRule) {"
    "if (mySheet.addRule) {"
    "mySheet.addRule(selector, newRule);"                               // For Internet Explorer
    "} else {"
    "ruleIndex = mySheet.cssRules.length;"
    "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"   // For Firefox, Chrome, etc.
    "}"
    "}";


    NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", [currentTextSize floatValue]];
    NSString *setFont = [NSString stringWithFormat:@"addCSSRule('body', 'font-family:%@;')", fontName];


    [theWebView stringByEvaluatingJavaScriptFromString:varMySheet];

    [theWebView stringByEvaluatingJavaScriptFromString:addCSSRule];

    [theWebView stringByEvaluatingJavaScriptFromString:setTextSizeRule];

    [theWebView stringByEvaluatingJavaScriptFromString:setFont];
}

07-28 01:54
查看更多