Brent
Brent

Reputation: 2485

Remove Html Format from String

I have been trying to format a string in my text view but I cant work it out. Im very new to xcode.

Am i missing something in this file? I have been looking through stack and this is how you do it..but its not working.

enter image description here

- (NSString *)stripTags:(NSString *)str
{
    NSMutableString *html = [NSMutableString stringWithCapacity:[str length]];

    NSScanner *scanner = [NSScanner scannerWithString:str];
    scanner.charactersToBeSkipped = NULL;
    NSString *tempText = nil;

    while (![scanner isAtEnd])
    {
        [scanner scanUpToString:@"<" intoString:&tempText];

        if (tempText != nil)
            [html appendString:tempText];

        [scanner scanUpToString:@">" intoString:NULL];

        if (![scanner isAtEnd])
            [scanner setScanLocation:[scanner scanLocation] + 1];

        tempText = nil;
    }

    return html;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *str = newsArticle;

    descTextView.text = [NSString stringWithString:str];


    // Do any additional setup after loading the view from its nib.
}

Upvotes: 1

Views: 2749

Answers (3)

Kuldeep Singh
Kuldeep Singh

Reputation: 334

@try this one

-(NSString *) stringByStrippingHTML:(NSString *)HTMLString {

    NSRange r;
    while ((r = [HTMLString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        HTMLString = [HTMLString stringByReplacingCharactersInRange:r withString:@""];
    return HTMLString;
}

Upvotes: 0

Jake Sankey
Jake Sankey

Reputation: 5137

This code is a modified version of what was posted as an answer to a similar question here https://stackoverflow.com/a/4886998/283412. This will take your HTML string and strip out the formatting.

-(void)myMethod
{
   NSString* htmlStr = @"<some>html</string>";
   NSString* strWithoutFormatting = [self stringByStrippingHTML:htmlStr];
}

-(NSString *)stringByStrippingHTML:(NSString*)str
{
  NSRange r;
  while ((r = [str rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
  {
     str = [str stringByReplacingCharactersInRange:r withString:@""];
  }
  return str;
}

Upvotes: 3

Paul Cezanne
Paul Cezanne

Reputation: 8741

You are trying to put HTML into a label. You want to use a UIWebView.

Upvotes: 1

Related Questions