改变webview字体的大小

作者: PM_崧崧 | 来源:发表于2016-09-07 11:11 被阅读2785次
    通过导航栏上得+-按钮 改变文字的大小

    1.首先创建导航栏按钮

    UIBarButtonItem* itemone = [[UIBarButtonItemalloc]initWithImage:[UIImageimageNamed:@"字体加大-1"]style:UIBarButtonItemStyleDonetarget:selfaction:@selector(addLarge)];

    UIBarButtonItem* itemtwo = [[UIBarButtonItemalloc]initWithImage:[UIImageimageNamed:@"字体减小-1"]style:UIBarButtonItemStyleDonetarget:selfaction:@selector(addmix)];

    self.navigationItem.rightBarButtonItems=@[itemone,itemtwo];

    self.navigationItem.rightBarButtonItem.tintColor= [UIColorwhiteColor];

    2.创建局部变量(分别是控制大小的变量,和通过接口获取的内容)

    @property(nonatomic,assign)int number;

    @property(nonatomic,copy)NSString* coment;

    3.初始化number =100

    _number=100;

    4.对coment赋值

    _coment=[NSStringstringWithFormat:@"%@",result[@"result"][@"content"]];   这里是从接口获取标签的内容。展示在webview上。

    5.点击增加 和减小的方法

    - (void)addLarge {

    if(_number<150)

    {

    _number+=10;

    [self.webViewloadHTMLString:_comentbaseURL:nil];

    }}

    -(void)addmix

    {

    if(_number>60)

    {

    _number-=10;

    [self.webViewloadHTMLString:_comentbaseURL:nil];

    }}

    6.最后一步啦(再加载结束后,对webview的字体改变)

    - (void)webViewDidFinishLoad:(UIWebView*)webView {

    NSString* stre = [NSString stringWithFormat:@"%d",_number];

    stre = [stre stringByAppendingFormat:@"%@",@"%"];;

    NSString* str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%@'",stre];

    [UIApplicationsharedApplication].networkActivityIndicatorVisible=NO;

    [self.webViewstringByEvaluatingJavaScriptFromString:str];

    }

    相关文章

      网友评论

        本文标题:改变webview字体的大小

        本文链接:https://www.haomeiwen.com/subject/olsjettx.html