Jeyhun Karimov Jeyhun Karimov - 4 months ago 54
iOS Question

iOS increase UIWebView font size

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?

Answer

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