Translate

Sunday, March 18, 2012

webview text alignment javascript for html content in iphone sdk

textAlign.js
//text align left
function leftAlign(){
var styleSheets = document.styleSheets;
for( i=0; i < styleSheets.length; i++ ){
    for( j=0; j < styleSheets[i].cssRules.length; j++){
        var rule = styleSheets[i].cssRules[j];           
        rule.style.textAlign='left';
        }
}
}
//text align right
function rightAlign(){
var styleSheets = document.styleSheets;
for( i=0; i < styleSheets.length; i++ ){
    for( j=0; j < styleSheets[i].cssRules.length; j++){
        var rule = styleSheets[i].cssRules[j];           
        rule.style.textAlign='right';
        }
}
}
//text align center
function centerAlign(){
var styleSheets = document.styleSheets;
for( i=0; i < styleSheets.length; i++ ){
    for( j=0; j < styleSheets[i].cssRules.length; j++){
        var rule = styleSheets[i].cssRules[j];           
        rule.style.textAlign='center';
        }
}
}
//text align justify
function justifyAlign(){
var styleSheets = document.styleSheets;
for( i=0; i < styleSheets.length; i++ ){
    for( j=0; j < styleSheets[i].cssRules.length; j++){
        var rule = styleSheets[i].cssRules[j];           
        rule.style.textAlign='justify';
        }
}
}

NSString *path = [[NSBundle mainBundle] pathForResource:@"textAlign" ofType:@"js"];
        NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        [webView stringByEvaluatingJavaScriptFromString:jsCode];
       
        if ([self.alignment isEqualToString:@"Justify"])
        {
            NSString *startSearch = [NSString stringWithFormat:@"justifyAlign();"];   
            [webView stringByEvaluatingJavaScriptFromString:startSearch];
        }
        else if ([self.alignment isEqualToString:@"Right"])
        {
            NSString *startSearch = [NSString stringWithFormat:@"rightAlign();"];   
            [webView stringByEvaluatingJavaScriptFromString:startSearch];
        }
        else if ([self.alignment isEqualToString:@"Left"])
        {
            NSString *startSearch = [NSString stringWithFormat:@"leftAlign();"];   
            [webView stringByEvaluatingJavaScriptFromString:startSearch];
        }
        else if ([self.alignment isEqualToString:@"Center"])
        {
            NSString *startSearch = [NSString stringWithFormat:@"centerAlign();"];   
            [webView stringByEvaluatingJavaScriptFromString:startSearch];
        } 

No comments:

Post a Comment