1

I'm trying to change Font in UIWebView. I tried with the following codes to change Font.

NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *cssPath = [path stringByAppendingPathComponent:@"test.css"]; 

    if(!cssPath)
    {
        NSLog(@"NO");
    }

    NSString *js = [NSString stringWithFormat:@"var cssChild = document.createElement('link');"
                    "cssChild = 'text/css';"
                    "cssChild = 'stylesheet';"
                    "cssChild = '%@';", cssPath];
    js = [NSString stringWithFormat:@"%@ document.getElementsByTagName('head')[0].appendChild(cssChild);", js];

    [webViewOfInitial stringByEvaluatingJavaScriptFromString:js];

I append css file into header of loaded page from internet. my css file is following like that.

@font-face {
    font-family: 'Zawgyi-One';
    font-style: normal;
    src: local('Zawgyi-One');
}

body {
    font-family: 'Zawgyi-One';
}

However when i retrieve html tags when finished page loaded , i can't find my above css tags. So how do i append that css file into current page of UIWebView's header tags? Or is there any others tutorials for UIWebView Font change?

Thanks.

1 Answer 1

2

You are going in the right direction but you haven't mentioned , which delegate method you are trying to make changes in. you can have your javascript in a .js file as you might make changes to it later and it will be really cumbersome to reflect the changes through code (instead of a js file).Try Using this :-

you have to load the HTML into the view with the correct baseURL parameter to use relative paths or files in UIWebView.

NSString *path = [[NSBundle mainBundle] bundlePath]; 
NSURL *baseURL = [NSURL fileURLWithPath:path];     
[webView loadHTMLString:htmlString baseURL:baseURL]; 

baseURL accepts an NSURL object as the file reference.You can specify a local URL or a web one as your needs leads you to.Append the css file in webViewDidFinishLoad:(UIWebView *)webView as you have done.

NOTE:-Select the .js file and uncheck the bullseye column indicating it as compiled code.(You will just get a warning if you don't).

Here's aUIWebview Javascript Tutorial that might get you going.

Sign up to request clarification or add additional context in comments.

4 Comments

If you could, please write a sample project for my question with static HTML Page. I really need. Please.
htmlstring is your html content ..either it can be a NSString or AN NSString pointing directly to a html file
Could you help me with sample project?I really need your help.Please.
HI , i cannot help you with project as i am myself doing one but i am gonna edit a link into my post ..see it ..it has all you need from root..to end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.