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
  • Parse HTML and fetch data from elements. Commented Feb 7, 2013 at 16:55
  • Sorry could you please explain how i do this? Commented Feb 7, 2013 at 16:57
  • see this raywenderlich.com/14172/how-to-parse-html-on-ios .. i guess you may not to be need apart from this. Commented Feb 7, 2013 at 17:05

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Seems for the sake of simply stripping out some HTML format, regex should be fine. I've used it in similar situations with success.
how does void myMethod effect whats before? I dont understand
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)
1

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

4 Comments

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
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...
All i want it todo is to display plain text, i didn't realise it would be this hard.
@BrentFrench HTML is not plain text. Extracting plain text from HTML is non-trivial.
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;
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.