0

I use UIWebView to display search test in Myanmar font like this

    NSString *googleText = @"မြန်မာဘာသာ";
    NSString *googleLink = [NSString stringWithFormat:@"http://www.google.com/search?hl=my&lr=lang_my&q=%@",
                            [[googleText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"
                             ]
                            ];

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:googleLink]]];

This is the result enter image description here

Is there any way that we can inject custom font into UIWebView when load content from a server?

I'm not load html files in my bundle, so I can't use custom font in CSS like this question or this one

Please help! Thanks!

Community
  • 1
  • 1
LE SANG
  • 10,955
  • 7
  • 59
  • 78
  • @Maulik I already check this question before.This way load a html file inside bundle where we can modify CSS. My question about load web content from google – LE SANG Dec 24 '13 at 11:42

2 Answers2

2

You can refer following Working code .

NSString *cssString = [[[[@"body { font-family:" stringByAppendingString:centuryGothicRegular] stringByAppendingString:@"; font-size:"] stringByAppendingString:[NSString stringWithFormat:@"%ld",normalFont]] stringByAppendingString:@"}\""] ; //1

NSString *javascriptString = @"var style = document.createElement('style'); style.innerHTML = '%@'; document.head.appendChild(style)"; // 2
NSString *javascriptWithCSSString = [NSString stringWithFormat:javascriptString, cssString]; // 3
NSLog(@"javascriptWithCSSString: \n %@", javascriptWithCSSString);
NSString *loadedContent = [webView stringByEvaluatingJavaScriptFromString:javascriptWithCSSString];
NSLog(@"loaded ocntent: %@", loadedContent);

Font name and size are declare as constant you can change according to your required font

Kiran Patil
  • 402
  • 5
  • 13
0

Previously answered and I have use custom fond by adding the font file

Using a UIWebView, can I use a custom font?

It is possible, but you need to make certain to add the Fonts provided by application key-value pair to the Info.plist.

I learned about it in this tutorial: http://tetontech.wordpress.com/2010/09/03/using-custom-fonts-in-your-ios-application/

I tried it with a LWFM and a FFIL font suitcase, but this didn't work. In the end I needed the TTF file of the font.

Community
  • 1
  • 1
Retro
  • 3,985
  • 2
  • 17
  • 41
  • Thanks! But this not answer my issue! I load content from google.com and wanna display correctly font. I can't add my custom font to CSS this way – LE SANG Dec 24 '13 at 14:06