Cocoanetics / DTFoundation

Standard toolset classes and categories
BSD 2-Clause "Simplified" License
805 stars 237 forks source link

Retaining non-html tags in text? #120

Closed guidedways closed 6 years ago

guidedways commented 6 years ago

Given the following code:

- (NSAttributedString *)attributedStringFromHTML:(NSString *)html
{
  NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
  DTHTMLAttributedStringBuilder*stringBuilder = [[DTHTMLAttributedStringBuilder alloc] initWithHTML:data options:nil documentAttributes:NULL];

  return [stringBuilder generatedAttributedString];
}

NSAttributedString *string = [self attributedStringFromHTML:@"Need to <b>contact</b> John <john@email.com> using Skype <http://www.skype.com>"];

NSString *plain = [string string];
NSLog(@"%@", plain);

I get:

Need to contact John using Skype

I need this to be:

Need to contact John <john@email.com> using Skype <http://www.skype.com>

Is this possible? I need to retain any non-html 'tags'. Input is free-form notes from a user that I need to parse effectively turning HTML into plainText.