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?

Santosh
  • 1,254
  • 2
  • 16
  • 31
  • Is it well formed HTML? Show an example of what you can expect. What in the HTML is guaranteed to be there? – Wain Sep 11 '15 at 12:12
  • actually there is no specific format for the html, any tag within the text would be considered as html – Santosh Sep 11 '15 at 12:16
  • Maybe with comparing lengths: `if ([[htmlString dataUsingEncoding:NSUnicodeStringEncoding] length]` == `[attributedString length]){//No Tags HTML have been interpreted}` – Larme Sep 11 '15 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 – Santosh Sep 11 '15 at 12:26
  • Regex for any tag then, but if there is a 'tag like' piece of text you will get a false positive – Wain Sep 11 '15 at 12:29
  • What do you mean change the size? `length` count the characters, not the "visual size". Or I'm missing something. – Larme Sep 11 '15 at 12:30
  • Actually attributed string includes the details including the properties of color and other Attribute string property details – Santosh Sep 11 '15 at 12:33
  • `length` property of `NSAttributedString` should return `[string length]` of the `NSAttributedString`. – Larme Sep 11 '15 at 13:29

1 Answers1

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.

Pablo A.
  • 2,042
  • 1
  • 17
  • 27
  • Actually it may be possible that the string only contains a tag or a

    tag. It's not important that the string is a proper formatted HTML string.
    – Santosh Sep 11 '15 at 12:29
  • 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... – Pablo A. Sep 11 '15 at 12:32
  • You may use a regular expression to match the html text, this link may be useful http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx/ and then apply the regular extension to your `NSString` as showed here http://stackoverflow.com/questions/9661690/use-regular-expression-to-find-replace-substring-in-nsstring – Pablo A. Sep 11 '15 at 12:52