Skip to content Skip to sidebar Skip to footer

How To Load Html In Uitextview?

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

Solution 1:

You can convert an HTML document to NSAttributedString like so:

// The HTML needs to be in the form of an NSStringNSError *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.

Post a Comment for "How To Load Html In Uitextview?"