Skip to content

Instantly share code, notes, and snippets.

@wess
Created December 6, 2012 20:26
Show Gist options
  • Save wess/4228027 to your computer and use it in GitHub Desktop.
Save wess/4228027 to your computer and use it in GitHub Desktop.
parsing HTML
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:string attributes:self.attributes];
TFHpple *doc = [[TFHpple alloc] initWithHTMLData:[[string lowercaseString] dataUsingEncoding:NSUTF8StringEncoding]];
NSArray *elements = [doc searchWithXPathQuery:@"//b | //strong | //em | //i"];
[elements enumerateObjectsUsingBlock:^(TFHppleElement *element, NSUInteger idx, BOOL *stop) {
NSString *tagName = [element.tagName lowercaseString];
if([tagName isEqualToString:@"b"] || [tagName isEqualToString:@"strong"])
{
NSRange boldRange = [string rangeOfString:element.text];
NSLog(@"B RANGE: %@", NSStringFromRange(boldRange));
[mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:self.boldFont.fontName size:self.font.pointSize] range:boldRange];
}
else if([tagName isEqualToString:@"i"] || [tagName isEqualToString:@"em"])
{
NSRange italicRange = [string rangeOfString:element.text];
NSLog(@"EM RANGE: %@", NSStringFromRange(italicRange));
NSLog(@"EM STRING: %@", [string substringWithRange:italicRange]);
[mutableAttributedString addAttribute:NSFontAttributeName value:self.headlineFont range:italicRange];
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment