2

I looked for solution on Stack Overflow but I didn't find one. My code is:

CGRect  rect = _postContentView.bounds;
_postContentUIWV = [[UIWebView alloc] initWithFrame:rect];
[_postContentUIWV loadHTMLString:selectedPostCD.content baseURL:nil];

int fontSize = 30;
NSString *jsString = [[NSString alloc] initWithFormat:
    @"document.getElementsByTagName('span')[0].style.webkitTextSizeAdjust='%d%%'",
    fontSize];
[_postContentUIWV stringByEvaluatingJavaScriptFromString:jsString];

[_postContentView addSubview:_postContentUIWV];

And the part of HTML inside UIWebView:

<p style="text-align: justify;">
    <span style="line-height: 1.5em;">blablablablablabla</span>
</p>

My code doesn't increase the font size inside UIWebView. How can I solve it?

Albzi
  • 15,431
  • 6
  • 46
  • 63
Jeyhun Karimov
  • 1,295
  • 19
  • 27

3 Answers3

4

Leave the code related to creation and initialization of _postContentUIWV inside the viewDidLoad method:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect  rect = _postContentView.bounds;
    _postContentUIWV = [[UIWebView alloc] initWithFrame:rect];
    _postContentUIWV.delegate = self;
    [_postContentUIWV loadHTMLString:selectedPostCD.content baseURL:nil];
    [_postContentView addSubview:_postContentUIWV];
}

Move the code related to evaluation of jsString to the webViewDidFinishLoad method:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    int fontSize = 30;
    NSString *jsString = [[NSString alloc] initWithFormat:
        @"document.getElementsByTagName('span')[0].style.webkitTextSizeAdjust='%d%%'",
        fontSize];
    [_postContentUIWV stringByEvaluatingJavaScriptFromString:jsString];
}
Lebyrt
  • 1,376
  • 1
  • 9
  • 18
  • Worked like a charm, you are my lifesaver – Jeyhun Karimov Apr 22 '14 at 15:21
  • Lots of info on how to adjust web text size- but I needed this for saving and restoring the user's preference! If you have multiple webviews like me- see this: http://stackoverflow.com/questions/5553610/multiple-uiwebviews-how-do-i-track-webviewdidfinishload-for-both – Mischa Apr 19 '16 at 03:34
1

Moreover, you can concatenate string to existing HTML inside UIWebView:

NSString * htmlString = @"<style>p {font-size:28px}</style>";
makeBiggerCssString = [makeBiggerCssString stringByAppendingString:
                       selectedPostCD.content];
[_postContentUIWV loadHTMLString: htmlString baseURL:nil];
kba
  • 19,333
  • 5
  • 62
  • 89
Jeyhun Karimov
  • 1,295
  • 19
  • 27
1

Try this method:

NSString *htmlString =[NSString stringWithFormat:@"<font face='Font_name' size='30'>%@", Yourstring];
[webview loadHTMLString:htmlString baseURL:nil];
Abhi
  • 243
  • 4
  • 19