/SIMarkdown

MarkdownView

Primary LanguageObjective-CMIT LicenseMIT

SIMarkdown

Language License

利用WKWebView实现简单的Markdown数据解析,核心利用了bootstrap进行解析

导入

将项目中MarkdownView文件夹拖到项目里

或者使用Pod安装

pod 'SIMarkdown', '~> 1.0'	

使用

  1. 导入头文件

    #import "MediaMetaManager.h"
  2. 初始化

    SIMarkdownView *markdownView = [[SIMarkdownView alloc] initWithFrame:self.view.bounds] ;
    markdownView.scrollEnabled = YES ;  // 是否可以滑动
    markdownView.showsScrollIndicator = NO ;  // 是否显示滑动指示器
  3. 根据需求设置回调

    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 ;
    } ; // 捕捉点击回调
  4. 加载本地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] ;
    }
  5. 加载网络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) ;
    }];
  6. 自定义显示的WebView,必须在加载网络资源之后调用

    markdownView.webView.UIDelegate = self ;

要求

iOS 8 or later.

SIMarkdown

bootstrap is licensed under MIT license.

SIMarkdown is available under the MIT license. See the LICENSE file for more info.