如何在UITextView中加载HTML?

我有一个带有achor标签的HTML内容。 我只是想在UITextView加载HTML内容。

您可以像这样将HTML文档转换为NSAttributedString

 // The HTML needs to be in the form of an NSString NSError *error = nil; NSAttributedString *attString = [[NSAttributedString alloc] initWithData:[HTML dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)} documentAttributes:nil error:&error]; if (error) { NSLog(@"Error: %@ %s %i", error.localizedDescription, __func__, __LINE__); } else { // Clear text view textView.text = @""; // Append the attributed string [textView.textStorage appendAttributedString:attString]; } 

如果遇到任何问题,请尝试在所有地方将编码更改为NSASCIIStringEncoding