天天看點

iOS小技能:加載本地HTML、pdf、doc、excel檔案 & HTML字元串與富文本互轉

前言

  • iOS加載本地HTML、pdf、doc、excel檔案,可采用WebView或者UIDocumentInteractionController進行實作。
  • HTML字元串與富文本互轉
應用場景:使用原生視圖UILabel顯示服務端傳回的帶有HTML标簽的内容

I 加載本地HTML檔案

當你在手機打開html檔案的時候,是不是用以下這個方法

将它作為郵件的附件,在手機端選擇其他應用打開,将html檔案存儲到檔案的iCloud/本機

再根據檔案名稱打開即可

如果你有需求在手機端打開本地html的需求,又覺得使用其他方法麻煩或者不管用的時候,推薦你可以自己寫個簡單的app進行打開。

1.1 原理

使用​

​[_webView loadHTMLString:html baseURL:baseURL];​

​ 進行代碼加載

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];


        [self setupWebViewController:  [[NSBundle mainBundle] pathForResource:KNUserGuideURL ofType:@"html"]  ];


//    [self setupAXWebViewController:  [[NSBundle mainBundle] pathForResource:KNUserGuideURL ofType:@"html"]  ];

}

- (void)setupWebViewController:(NSString*)path{




    NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    [_webview loadHTMLString:html baseURL:baseURL];// 進行代碼加載      

1.2 源碼

  • ​​demo源碼下載下傳​​

1、​​從CSDN下載下傳源碼位址:https://download.csdn.net/download/u011018979/15448928​​​

2、​​​private 倉庫​​​:​​github.com/zhangkn/NSA…​​​

3、【private 倉庫】​​​github.com/zhangkn/loa…​​​

相關文章:​​​blog.csdn.net/z929118967/…​​

1.3 核心代碼

  • 通過檔案名擷取path
NSBundle mainBundle] pathForResource:KNUserGuideURL ofType:@"html"]  ];// 通過檔案名擷取path      
  • 根據path進行代碼的加載
- (void)setupAXWebViewController:(NSString*)path{




    NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];


    AXWebViewController *webVC = [[AXWebViewController alloc] initWithHTMLString:html baseURL:baseURL];




    webVC.showsToolBar = NO;
    webVC.navigationController.navigationBar.translucent = NO;
    webVC.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.100f green:0.100f blue:0.100f alpha:0.800f];
    webVC.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.996f green:0.867f blue:0.522f alpha:1.00f];

    UINavigationController *tmp = [[UINavigationController alloc]initWithRootViewController:webVC];

    [self presentViewController:tmp animated:YES      

II 加載pdf、doc、excel檔案

2.1 使用WebView打開

  1. iOS12之前使用UIWebView
void)openpdfByWebView{


    NSString *path = [[NSBundle
                       mainBundle] pathForResource:@"ios.pdf"
                      ofType:nil];
    NSURL *url = [NSURL
                  fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest
                             requestWithURL:url];
    [self.webView
     loadRequest:request];

}      
  1. iOS12之後采用WKWebView
- (void)openpdfByWKWebView{

    WKWebView *tmp = [WKWebView new] ;


    self.wk_webView =tmp;
    [self.view addSubview:self.wk_webView];

    self.wk_webView.frame = self.view.frame;


    NSString *path = [[NSBundle
                       mainBundle] pathForResource:@"ios.pdf"
                      ofType:nil];
    NSURL *url = [NSURL
                  fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest
                             requestWithURL:url];
    [self.wk_webView
     loadRequest:request];


}      

2.2 使用文檔控制器進行檔案預覽、分享、列印、存儲到手機

​​kunnan.blog.csdn.net/article/det…​​

III HTML字元串與富文本互轉

demo源碼下載下傳:​​download.csdn.net/download/u0…​​

3.1 html轉換為富文本

NSString *html = @"<p style='color:green'>部落格<span style='color:#e83c36;'><a>https://kunnan.blog.csdn.net/<a/></span><br/>微信公衆号:<span style='color:red'>iOS逆向</span><br/>“訂閱”一次 ,享受終身服務的快樂。</br><span style='color:red'>專注《iOS應用逆向與安全》</span>(包括iOS基礎)</p>";
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];      

3.2 富文本轉換為html

- (void)NSAttributedStringtohtmlWith:(NSAttributedString*)attStr{

    NSDictionary *dic = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:@(NSUnicodeStringEncoding)};


    NSData *data = [attStr dataFromRange:NSMakeRange(0, attStr.length) documentAttributes:dic error:nil];



    NSString *str = [[NSString alloc] initWithData:data encoding:NSUnicodeStringEncoding];

    NSLog(@"NSAttributedStringtohtmlWith:%@",str);

    [self