NSString *htmlString = @“传入的字符串”
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@""" withString:@"\""];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@">" withString:@">"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&" 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:@"自己服务器基地址"]];
});
网友评论