poojathorat
poojathorat

Reputation: 1220

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. here is the code how I change the font of 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];

Upvotes: 0

Views: 659

Answers (2)

Fayza Nawaz
Fayza Nawaz

Reputation: 2316

Although the post is too old, and above answer helped me a lot, but i faced issues while doing this in 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. Default value is 100 and you can increase/decrease that.

For simply updating the fontSize, i'm using "-webkit-text-size-adjust" property. and the following piece of code is enough for that:

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

Upvotes: 0

Numeral
Numeral

Reputation: 1405

Check this project https://github.com/fedefrappi/AePubReader . You can simply store fontName and fontSize values in NSUserDefaults and add CSS rules in webView delegate method webViewDidFinishLoad:

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

Upvotes: 2

Related Questions