diff --git a/src/XNGMarkdownParser.h b/src/XNGMarkdownParser.h index 2a6a77d..b8aba00 100644 --- a/src/XNGMarkdownParser.h +++ b/src/XNGMarkdownParser.h @@ -58,6 +58,8 @@ typedef NS_ENUM (NSUInteger, XNGMarkdownParserHeader) { @property (nonatomic, copy) NSString *linkFontName; // Default: paragraphFont @property (nonatomic, assign) BOOL shouldParseLinks; // Default: YES +@property (nonatomic, strong) UIColor *headerTextColor; // TextColor for header + // common attributes that affect the whole string, can be overriden by the upper attributes @property (nonatomic, strong) NSDictionary *topAttributes; // default: nil (do nothing) diff --git a/src/XNGMarkdownParser.m b/src/XNGMarkdownParser.m index f6edc9d..80e1aa7 100644 --- a/src/XNGMarkdownParser.m +++ b/src/XNGMarkdownParser.m @@ -227,13 +227,22 @@ - (NSDictionary *)attributesForFont:(UINSFont *)font { } - (void)recurseOnString:(NSString *)string withFont:(UINSFont *)font { + [self recurseOnString:string withFont:font withTextColor:nil]; +} + +- (void)recurseOnString:(NSString *)string withFont:(UINSFont *)font withTextColor:(UIColor *)textColor { XNGMarkdownParser *recursiveParser = [self copy]; recursiveParser->_topFont = font; - - NSAttributedString *recursedString = [recursiveParser attributedStringFromMarkdownString:string]; + + NSAttributedString *recursedString =[recursiveParser attributedStringFromMarkdownString:string]; NSMutableAttributedString *mutableRecursiveString = [[NSMutableAttributedString alloc] initWithAttributedString:recursedString]; - [mutableRecursiveString addAttributes:@{NSFontAttributeName: font} - range:NSMakeRange(0, recursedString.length)]; + if (textColor) { + [mutableRecursiveString addAttributes:@{NSFontAttributeName : font, NSForegroundColorAttributeName : textColor} + range:NSMakeRange(0, recursedString.length)]; + } else { + [mutableRecursiveString addAttributes:@{NSFontAttributeName : font} + range:NSMakeRange(0, recursedString.length)]; + } [_accum appendAttributedString:mutableRecursiveString]; } @@ -299,7 +308,7 @@ - (void)consumeToken:(XNGMarkdownParserCode)token text:(char *)text { textAsString = [[textAsString substringFromIndex:rangeOfNonHash.location] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; XNGMarkdownParserHeader header = (XNGMarkdownParserHeader)(rangeOfNonHash.location - 1); - [self recurseOnString:textAsString withFont:[self fontForHeader:header]]; + [self recurseOnString:textAsString withFont:[self fontForHeader:header] withTextColor:self.headerTextColor]; // We already appended the recursive parser's results in recurseOnString. textAsString = nil; @@ -316,8 +325,8 @@ - (void)consumeToken:(XNGMarkdownParserCode)token text:(char *)text { } else if ([[components objectAtIndex:1] rangeOfString:@"-"].length > 0) { font = [self fontForHeader:XNGMarkdownParserHeader2]; } - - [self recurseOnString:textAsString withFont:font]; + + [self recurseOnString:textAsString withFont:font withTextColor:self.headerTextColor]; // We already appended the recursive parser's results in recurseOnString. textAsString = nil;