简体   繁体   English

iPhone ePub Reader更改ePub中所有页面的字体,而不是当前的webview内容

[英]iPhone ePub Reader Change font of all pages in ePub book not current webview contents

I am developing iphone app like ibook app.I load the contents of XML file that I get after parsing the epub file in UIWebView .I changed the font size & font family of contents of UIWebView .I want to change the font size & font family of all the pages in ePub book.How to store the settings of UIWebView so that it is applied to all pages in EPub book. 我喜欢开发的iBook app.I iPhone应用程序加载的内容XML文件解析我在EPUB文件后,得到UIWebView 。我改变了内容的字体大小和字体系列UIWebView 。我想改变字体大小和字体系列ePub book中的所有页面。如何存储UIWebView的设置,以便它应用于EPub书中的所有页面。 here is the code how I change the font of weview . 这是代码我如何改变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];

Check this project https://github.com/fedefrappi/AePubReader . 检查此项目https://github.com/fedefrappi/AePubReader You can simply store fontName and fontSize values in NSUserDefaults and add CSS rules in webView delegate method webViewDidFinishLoad: 您可以在NSUserDefaults中简单地存储fontName和fontSize值,并在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];
}

Although the post is too old, and above answer helped me a lot, but i faced issues while doing this in Swift . 虽然帖子太旧了,但上面的答案对我帮助很大,但我在Swift中这样做时遇到了问题。 So, may be this answer can save someone's time. 所以,这个答案可能可以节省一些人的时间。

let varMySheet = "var mySheet = document.styleSheets[0];"

let addCSSRule = "function addCSSRule(selector, newRule) {if (mySheet.addRule) {mySheet.addRule(selector, newRule);} else { ruleIndex = mySheet.cssRules.length;mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);}}"

let setTextSizeRule = "addCSSRule('body', '-webkit-text-size-adjust: \(fontPercentage)%;')"


self.webView.stringByEvaluatingJavaScriptFromString(varMySheet)
self.webView.stringByEvaluatingJavaScriptFromString(addCSSRule)
self.webView.stringByEvaluatingJavaScriptFromString(setTextSizeRule)

fontPercentage is a variable in my class. fontPercentage是我班级中的一个变量。 Default value is 100 and you can increase/decrease that. 默认值为100,您可以增加/减少该值。

For simply updating the fontSize, i'm using " -webkit-text-size-adjust " property. 为了简单地更新fontSize,我使用“ -webkit-text-size-adjust ”属性。 and the following piece of code is enough for that: 以下代码就足够了:

let textSizeRule = "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(fontPercentage)%'"
self.webView.stringByEvaluatingJavaScriptFromString(textSizeRule)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM