2

I have a HTML contents with achor tag in it. I just want to load that HTML content in UITextView.

rmaddy
  • 307,833
  • 40
  • 508
  • 550
Kirti Parghi
  • 1,067
  • 3
  • 13
  • 29

1 Answers1

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