iOS中OC加载HTML字符串
来源:互联网 发布:解密软件免费下载 编辑:程序博客网 时间:2024/06/06 10:44
最近项目里面遇见了 HTML 字符串,整理如下:
在 iOS 中通常加载 HTML 字符串有两种方式
- 通过 UILabel 加载富文本的方法加载 HTML 字符串
- 通过 WebView 加载 HTML 字符串
- (void)viewDidLoad { [super viewDidLoad];1.UILabel 加载 HTML 字符串NSString * str1 = @"<div>Google(中文名:谷歌),是一家美国的跨国科技企业。</div><div>Google由当时在斯坦福大学攻读理工博士的拉里·佩奇和谢尔盖·布卢姆共同创建,因此两人也被称为“Google Guys”。</div><div>1998年9月4日,Google以私营公司的形式创立,设计并管理一个互联网搜索引擎“Google搜索”。</div>";NSString * str2 = @"<p><br></p>";NSString * str3 = @"<p>qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq</p>";//1.将字符串转化为标准HTML字符串 str1 = [self htmlEntityDecode:str1];//2.将HTML字符串转换为attributeString NSAttributedString * attributeStr = [self attributedStringWithHTMLString:str1];//3.使用label加载html字符串 self.label.attributedText = attributeStr;2.UIWebView 加载HTML字符串 UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectMake(20, 300, self.view.frame.size.width - 40, 400)]; [webView loadHTMLString:str1 baseURL:nil]; [self.view addSubview:webView]; self.webView = webView;}//将 < 等类似的字符转化为HTML中的“<”等 - (NSString *)htmlEntityDecode:(NSString *)string{ string = [string stringByReplacingOccurrencesOfString:@""" withString:@"\""]; string = [string stringByReplacingOccurrencesOfString:@"'" withString:@"'"]; string = [string stringByReplacingOccurrencesOfString:@"<" withString:@"<"]; string = [string stringByReplacingOccurrencesOfString:@">" withString:@">"]; string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"&"]; // Do this last so that, e.g. @"&lt;" goes to @"<" not @"<" return string;}//将HTML字符串转化为NSAttributedString富文本字符串- (NSAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString{ NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) }; NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding]; return [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];}//去掉 HTML 字符串中的标签- (NSString *)filterHTML:(NSString *)html{ NSScanner * scanner = [NSScanner scannerWithString:html]; NSString * text = nil; while([scanner isAtEnd]==NO) { //找到标签的起始位置 [scanner scanUpToString:@"<" intoString:nil]; //找到标签的结束位置 [scanner scanUpToString:@">" intoString:&text]; //替换字符 html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""]; } // NSString * regEx = @"<([^>]*)>"; // html = [html stringByReplacingOccurrencesOfString:regEx withString:@""]; return html;}
注意
- 此处的字符串不是标准的标签的HTML字符串,所以我们首先要调用- (NSString )htmlEntityDecode:(NSString )string 方法将字符串转换成标准的HTML字符串,这样才可以进行HTML字符串的加载
- 实例的第二个字符串中的内容为空,当用 Label 加载的时候只是两行空白数据,此时可以调用- (NSString )filterHTML:(NSString)html 方法,去掉标签,将HTML字符串转换为常用的字符串样式
阅读全文
1 0
- iOS中OC加载HTML字符串
- iOS-OC中加载html5调用html方法和修改HTML5内容
- iOS-OC中加载html5调用html方法和修改HTML5内容
- android中使用WebView加载html字符串
- iOS中webview加载本地html文件
- iOS 之OC字符串
- IOS--OC--LessonNSString 字符串
- IOS OC 字符串
- iOS中HTML与OC的交互实现
- iOS 删除字符串中html标签
- iOS UIlabel怎么加载html字符串 富文本的用法
- iOS开发中对OC字符串的相关操作<转>
- iOS oc加载plist数据
- iOS OC/Swift懒加载
- iOS - html 与 oc交互
- uiwebView 加载html字符串
- OC中字符串NSstring
- iOS 加载本地html
- 共享单车竞争感悟
- 数据仓库建设
- Java的加密和解密艺术
- 界面的动画跳转
- 比较有用的网站
- iOS中OC加载HTML字符串
- 【代码笔记】iOS-播放从网络上下载的语音
- python web.py详解
- safari浏览器下 localStorage会报错的BUG
- C++ 虚函数表解析
- linux chmod的详细用法
- 『图论』LCA 最近公共祖先----Tarjan 算法
- ES6变量的解构赋值
- java switch 坑区