简体   繁体   中英

How can I change font color and font style of ePub Book in iOS?

I have created app of books which supports pdf and ePub format. In that app, pdf is just shown as an image and ePub open in webview because EPUB is just XHTML stored in a zipfile with an XML manifest. Now, I want to change the font style and font color of ePub book pages. Is it possible? If possible, then how can I achieve this? I have searched a lot, but did not get any proper solution.

I have found the solution. I have make a string and evaluate using Java string

This is what I have written:

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 *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];
NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];
NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", currentTextSize];
NSString *setHighlightColorRule = [NSString stringWithFormat:@"addCSSRule('highlight', 'background-color: yellow;')"];

    // this is what change the text style 

NSString *insertRule3 = [NSString stringWithFormat:@"addCSSRule('html, body, div, p, span, a', 'font-family: arial;')"];
NSString *changeColor = [NSString stringWithFormat:@"addCSSRule('html, body, div, p, span, a', 'color: #1122CC;')"];

[webView stringByEvaluatingJavaScriptFromString:varMySheet];

[webView stringByEvaluatingJavaScriptFromString:addCSSRule];

[webView stringByEvaluatingJavaScriptFromString:insertRule1];

[webView stringByEvaluatingJavaScriptFromString:insertRule2];

[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];

[webView stringByEvaluatingJavaScriptFromString:setHighlightColorRule];
    [webView stringByEvaluatingJavaScriptFromString:insertRule3];
    [webView stringByEvaluatingJavaScriptFromString:changeColor];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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