NSString *dataString = @"这是一个标题< br/>这是第二行";
//先去除转义字符
dataString = [dataString stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
dataString = [dataString stringByReplacingOccurrencesOfString:@">" withString:@">"];
dataString = [dataString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
//html 的meta最好加上<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no\">这样在移动设备显示起来效果更好
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>";
NSString *htmlContent = [NSString stringWithFormat:htmlFormat,dataString];
UIWebView *web=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height-64)];
web.scrollView.bounces=NO;//不弹跳
web.userInteractionEnabled=YES;
web.backgroundColor=[UIColor clearColor];
[web loadHTMLString:htmlContent baseURL:nil];
两个分类方法:
- 变成转义符的形式
- (NSString *) escapeHTML{
NSMutableString *s = [NSMutableString string];
NSUInteger start = 0;
NSUInteger len = [self length];
NSCharacterSet *chs = [NSCharacterSet characterSetWithCharactersInString:@"<>&\""];
while (start < len) {
NSRange r = [self rangeOfCharacterFromSet:chs options:0 range:NSMakeRange(start, len-start)];
if (r.location == NSNotFound) {
[s appendString:[self substringFromIndex:start]];
break;
}
if (start < r.location) {
[s appendString:[self substringWithRange:NSMakeRange(start, r.location-start)]];
}
switch ([self characterAtIndex:r.location]) {
case '<':
[s appendString:@"<"];
break;
case '>':
[s appendString:@">"];
break;
case '"':
[s appendString:@"""];
break;
// case '…':
// [s appendString:@"…"];
// break;
case '&':
[s appendString:@"&"];
break;
}
start = r.location + 1;
}
return s;
}
2. 去除转义符,变成普通的标签<>
- (NSString *) unescapeHTML{
NSMutableString *s = [[NSMutableString alloc] init];
NSMutableString *target = [self mutableCopy];
NSCharacterSet *chs = [NSCharacterSet characterSetWithCharactersInString:@"&"];
while ([target length] > 0) {
NSRange r = [target rangeOfCharacterFromSet:chs];
if (r.location == NSNotFound) {
[s appendString:target];
break;
}
if (r.location > 0) {
[s appendString:[target substringToIndex:r.location]];
[target deleteCharactersInRange:NSMakeRange(0, r.location)];
}
if ([target hasPrefix:@"<"]) {
[s appendString:@"<"];
[target deleteCharactersInRange:NSMakeRange(0, 4)];
} else if ([target hasPrefix:@">"]) {
[s appendString:@">"];
[target deleteCharactersInRange:NSMakeRange(0, 4)];
} else if ([target hasPrefix:@"""]) {
[s appendString:@"\""];
[target deleteCharactersInRange:NSMakeRange(0, 6)];
} else if ([target hasPrefix:@"'"]) {
[s appendString:@"'"];
[target deleteCharactersInRange:NSMakeRange(0, 5)];
}else if ([target hasPrefix:@"'"]) {
[s appendString:@"'"];
[target deleteCharactersInRange:NSMakeRange(0, 6)];
} else if ([target hasPrefix:@"&"]) {
[s appendString:@"&"];
[target deleteCharactersInRange:NSMakeRange(0, 5)];
} else if ([target hasPrefix:@"…"]) {
[s appendString:@"…"];
[target deleteCharactersInRange:NSMakeRange(0, 8)];
} else {
[s appendString:@"&"];
[target deleteCharactersInRange:NSMakeRange(0, 1)];
}
}
return s;
}
_content = [_content unescapeHTML];
_content = [NSString stringWithFormat:@"<html><head><meta name=\"viewport\" content=\"width=%f,initial-scale=1.0,maximum-scale=1.0,user-scalable=0\" /><style>img {max-width: 100%% ;height: auto;} \ntable {max-width: 100%% ;height: auto;}</style></head><body>%@</body></html>", (kScreen_Width - 30), _content];
网友评论