iOS Dev: Strip HTML tags from an NSString

As it turns out there is no single one liner for striping HTML tags from an NSString in the iOS SDK. So I've written the following NSString category that adds a stripHtml method to all NSStrings. To use this include the .h and .m file into your project and:

Import the header:
#import "NSString_stripHtml.h"

And then call stripHtml:
NSString* mystring = @"<b>Hello</b> World!!";
NSString* stripped = [mystring stripHtml];
// stripped will be = Hello World!!


5 comments:

  1. nice method. i'd like to replace break tags <-br-> (hyphens added to prevent rendering here) with a space rather than no space. but i'm not sure where to put the if logic for this exclusion. thanks for your help!

    ReplyDelete
  2. How can i download the files?

    ReplyDelete
  3. You can see the files now. There was a problem with the blog page and the github files weren't displaying.

    ReplyDelete
  4. It's great ... thanks

    ReplyDelete
  5. This worked better in my project than feeding the string through the NSAttributedString initWithHTM method. It produces a lot fewer empty lines. It is also a lot easier than playing around with the three20 framework. Thanks for your post.

    ReplyDelete