2

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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Kirti Parghi
  • 1,142
  • 3
  • 14
  • 31

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
  • 5,197
  • 3
  • 31
  • 58