ios用lable和textview載入html標籤

來源:互聯網
上載者:User

標籤:html標籤   做了   比較   bubuko   內容   des   str   完成   img   

根據應用需求,需要將後台傳過來的html標籤文本顯示在頁面上,首先想到的是用WKWebview顯示,雖然可以實現但是感覺比較耗記憶體。嘗試用lable載入html,代碼以及效果如下所示,發現表格的邊框樣式載入不出來

NSString *str = @"羅夢槐,您好!您的借款項目【0903.按月3.02】債權轉讓已經完成,當前持有資訊如下:<table  border=‘1‘ cellpadding=‘0‘ width=‘80%;‘ cellspacing=‘0‘><tr><td align=‘center‘>使用者名稱</td><td align=‘center‘>債權持有金額</td></tr><tr><td align=‘center‘>13944831072</td><td align=‘center‘>2200.00</td></tr><tr><td align=‘center‘>13944831077</td><td align=‘center‘>600.00</td></tr><tr><td align=‘center‘>13944831078</td><td align=‘center‘>200.00</td></tr><tr><td align=‘center‘>13944831078</td><td align=‘center‘>200.00</td></tr><tr><td align=‘center‘>13944831078</td><td align=‘center‘>1800.00</td></tr><tr><td align=‘center‘>13944831078</td><td align=‘center‘>800.00</td></tr><tr><td align=‘center‘>13944831079</td><td align=‘center‘>400.00</td></tr></table>";    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(15, 100, K_SCREEN_WIDHTH-30, 300)];lab.numberOfLines = 0;[self.view addSubview:lab];NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];lab.attributedText = attrStr;lab.font = [UIFont systemFontOfSize:15];

然後抱著試試的心態用textview載入html標籤發現邊框樣式可以顯示出來了,應該是底層內部對lab和textview做了不同的處理,代碼以及效果如下

UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(15, 400, K_SCREEN_WIDHTH-30, 300)];textView.userInteractionEnabled = NO;[self.view addSubview:textView];NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];textView.attributedText = attrStr;textView.font = [UIFont systemFontOfSize:15];

 

總結textView和lable都可以載入html標籤內容,但是textview相比於lable可以同時載入出標籤裡的樣式,當然通過webview也可以實現對html的載入但是相對更耗記憶體這裡不建議使用,希望對遇到同樣問題的朋友有協助

ios用lable和textview載入html標籤

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.