利用WKWebView实现简单的Markdown数据解析,核心利用了bootstrap进行解析
将项目中MarkdownView
文件夹拖到项目里
或者使用Pod
安装
pod 'SIMarkdown', '~> 1.0'
-
导入头文件
#import "MediaMetaManager.h"
-
初始化
SIMarkdownView *markdownView = [[SIMarkdownView alloc] initWithFrame:self.view.bounds] ; markdownView.scrollEnabled = YES ; // 是否可以滑动 markdownView.showsScrollIndicator = NO ; // 是否显示滑动指示器
-
根据需求设置回调
markdownView.renderedAction = ^(CGFloat height) { NSLog(@"Height:%lf",height) ; } ; // 获取结果回调 markdownView.linkTouchAction = ^BOOL(NSURLRequest *request) { NSURL *url = request.URL ; if (url) { if ([url.scheme isEqualToString:@"file"]) { return true ; }else if ([url.scheme isEqualToString:@"https"] || [url.scheme isEqualToString:@"http"]) { SFSafariViewController *sfvc = [[SFSafariViewController alloc]initWithURL:url] ; [self.navigationController pushViewController:sfvc animated:YES] ; return false ; } } return false ; } ; // 捕捉点击回调
-
加载本地markdown字符串
NSString *path = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"md"] ; NSError *error ; NSString *markStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error] ; if (!error) { [markdownView loadMarkdownString:markStr] ; }
-
加载网络markdown资源
[markdownView loadMarkdownWithURL:[NSURL URLWithString:@"https://raw.githubusercontent.com/matteocrippa/awesome-swift/master/README.md"]withSuccess:^(SIMarkdownView *markView, NSData *data) { markdownView.webView.scrollView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0) ; NSString *markdown = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding] ; NSLog(@"%@",markdown) ; }];
-
自定义显示的WebView,必须在加载网络资源之后调用
markdownView.webView.UIDelegate = self ;
iOS 8 or later.
bootstrap is licensed under MIT license.
SIMarkdown is available under the MIT license. See the LICENSE file for more info.