簡體   English   中英

NSString呈現HTML標簽?

[英]NSString render HTML tags?

我正在開發一個應用程序,顯示同樣顯示在應用程序的網頁版本上的內容。 我獲得了與網頁顯示相同的標記文本,但是在UITextView中。

該文本嵌入了HTML標記,例如<BR><p><em>用於break,paragraph和bold(emphasis)。

是否存在NSString或UITextView中的文本的編碼,可以呈現HTML標記,因為它們將顯示在網頁中? 我希望看到中斷,新段落,粗體等等,而不僅僅是剝離html標簽。 提前致謝。

您可以使用UIWebView在iPhone應用程序上查看HTML內容,而不是使用UITextView NSString存儲為字符串。 它沒有呈現它。

以下是在UIWebView中從字符串加載html的簡單代碼。 您可以使用其他屬性自定義Webview,以便在html中的鏈接上進行用戶交互,以實現UIWebView委托。

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 0, 300, 300)];

NSString *html = [NSString stringWithFormat:@"%@", YOUR_HTML_GOES_HERE];
[webView loadHTMLString:html baseURL:[NSURL URLWithString:@""]];

[self addSubview:webView];
[webView release];

您可以使用UITextView使用NSAttributedString呈現html,如下所示(在iOS 7.0及更高版本中可用):

NSAttributedString *attributedString = [[NSAttributedString alloc] 
initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] 
                                   options:@{
               NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType
                                            }
                        documentAttributes:nil
                                     error:nil];
textView.attributedText = attributedString;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM