2

I am using a UITextView to display text on a screen. I also want to display HTML text in the same TextView. What I want is, when the string contains any HTML data then it should be displayed according to HTML formatter otherwise textview should use the font defined by me.

Here is the code I am using to display the HTML text in the textview:

NSString *htmlString = promotion.content;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];     
self.promotionTextView.attributedText = attributedString;

I just want to check if the string that I am receiving in the htmlString contains any html data or not. How I can do that?

8
  • Is it well formed HTML? Show an example of what you can expect. What in the HTML is guaranteed to be there? Commented Sep 11, 2015 at 12:12
  • actually there is no specific format for the html, any tag within the text would be considered as html Commented Sep 11, 2015 at 12:16
  • Maybe with comparing lengths: if ([[htmlString dataUsingEncoding:NSUnicodeStringEncoding] length] == [attributedString length]){//No Tags HTML have been interpreted} Commented Sep 11, 2015 at 12:20
  • No man, actually the attributes that are appended at the end of attributed string changes its size. So the length is always different Commented Sep 11, 2015 at 12:26
  • Regex for any tag then, but if there is a 'tag like' piece of text you will get a false positive Commented Sep 11, 2015 at 12:29

1 Answer 1

0

I would look for html start/end labels and get the substring between them, something like this:

- (NSString *)htmlFromString:(NSString *)string
{
    NSString *htmlString = nil;

    if([string rangeOfString:@"<html>"].location != NSNotFound && [string rangeOfString:@"</html>"].location != NSNotFound) {

        NSRange firstRange = [string rangeOfString:@"<html>"];
        NSRange secondRange = [string rangeOfString:@"</html>"];

        if(firstRange.location < secondRange.location) {

            NSRange htmlRange = NSMakeRange(firstRange.location, secondRange.location + secondRange.length);
            htmlString = [string substringWithRange:htmlRange];
        }

    }

    return htmlString;
}

I haven't tried it, but it think it should work.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually it may be possible that the string only contains a <b> </b> tag or a <h1> </h1> tag. It's not important that the string is a proper formatted HTML string.
mmm I know what you mean... hard to know then, as html is simple tags and you don't know when they start or end without any mark...
You may use a regular expression to match the html text, this link may be useful haacked.com/archive/2004/10/25/… and then apply the regular extension to your NSString as showed here stackoverflow.com/questions/9661690/…

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.