I have a HTML contents with achor tag in it. I just want to load that HTML content in UITextView.
Asked
Active
Viewed 3,045 times
2
rmaddy
- 307,833
- 40
- 508
- 550
Kirti Parghi
- 1,067
- 3
- 13
- 29
-
View this http://stackoverflow.com/questions/18608060/get-html-in-uitextview – pcs Apr 26 '15 at 08:24
1 Answers
3
You can convert an HTML document to NSAttributedString like so:
// 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];
}
If you come across any problems then try changing the encoding to NSASCIIStringEncoding in all places.
Rob Sanders
- 4,818
- 3
- 28
- 56