iOS 转译H5代码放到Webview

2017-11-23  本文已影响149人  biubiuboom

NSString *htmlString = @“传入的字符串”

 htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
    htmlString = [@"<style>img{max-width:100%;height:auto;}</style>" stringByAppendingString:htmlString];
    NSString *htmlFormat = @"<!DOCTYPE html> <html lang=\"cn\"> <head> <meta charset=\"utf-8\"> <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no\"> </head> <body> %@ </body> </html>";
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *htmlContent = [NSString stringWithFormat:htmlFormat,htmlString];
        [self.webView loadHTMLString:htmlContent baseURL:[NSURL URLWithString:@"自己服务器基地址"]];
    });
上一篇下一篇

猜你喜欢

热点阅读