1

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.
}
3

3 Answers 3

3

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;
}
4
  • 1
  • Seems for the sake of simply stripping out some HTML format, regex should be fine. I've used it in similar situations with success. Feb 8, 2013 at 2:07
  • how does void myMethod effect whats before? I dont understand
    – Brent
    Feb 8, 2013 at 8:58
  • Brent, myMethod is just an example of a context where you can use this. The code in myMethod could be executed in any where you want (such as viewDidLoad) Feb 11, 2013 at 7:31
1

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

4
  • I have collected the data via json, i just want to display the text in the content tag there, as the website is not mobile friendly. Thanks
    – Brent
    Feb 7, 2013 at 16:52
  • That doesn't change the fact that you have HTML. You are going to have to parse your JSON, extract the parts you want, and then display them. That doesn't sound fun... Feb 7, 2013 at 17:03
  • All i want it todo is to display plain text, i didn't realise it would be this hard.
    – Brent
    Feb 7, 2013 at 17:09
  • 2
    @BrentFrench HTML is not plain text. Extracting plain text from HTML is non-trivial.
    – rmaddy
    Feb 7, 2013 at 17:26
0

@try this one

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.