美文网首页
iOS 转译H5代码放到Webview

iOS 转译H5代码放到Webview

作者: biubiuboom | 来源:发表于2017-11-23 18:08 被阅读149次

    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:@"自己服务器基地址"]];
        });
    

    相关文章

      网友评论

          本文标题:iOS 转译H5代码放到Webview

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