Skip to content
This repository was archived by the owner on Mar 8, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/XNGMarkdownParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
23 changes: 16 additions & 7 deletions src/XNGMarkdownParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down