Skip to content
Merged
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
27 changes: 25 additions & 2 deletions NSAttributedString+DDHTML/NSAttributedString+DDHTML.m
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,19 @@ + (NSAttributedString *)attributedStringFromNode:(xmlNodePtr)xmlNode normalFont:
if (strncmp("b", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0 ||
strncmp("strong", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0) {
if (boldFont) {
[nodeAttributedString addAttribute:NSFontAttributeName value:boldFont range:nodeAttributedStringRange];
if (![NSAttributedString applyBoldItalicToAttributedString:nodeAttributedString ifMatchFontIsPresent:italicFont forRange:nodeAttributedStringRange]) {
[nodeAttributedString addAttribute:NSFontAttributeName value:boldFont range:nodeAttributedStringRange];
}
}
}

// Italic Tag
else if (strncmp("i", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0 ||
strncmp("em", (const char *)xmlNode->name, strlen((const char *)xmlNode->name)) == 0) {
if (italicFont) {
[nodeAttributedString addAttribute:NSFontAttributeName value:italicFont range:nodeAttributedStringRange];
if (![NSAttributedString applyBoldItalicToAttributedString:nodeAttributedString ifMatchFontIsPresent:boldFont forRange:nodeAttributedStringRange]) {
[nodeAttributedString addAttribute:NSFontAttributeName value:italicFont range:nodeAttributedStringRange];
}
}
}

Expand Down Expand Up @@ -504,4 +508,23 @@ + (ListInfo *)getListInfoFromNode:(xmlNodePtr)xmlNode {
return listInfo;
}

+ (BOOL)applyBoldItalicToAttributedString:(NSMutableAttributedString *)attributedString ifMatchFontIsPresent:(UIFont *)matchFont forRange:(NSRange)range {
__block BOOL wasChanged = NO;

[attributedString enumerateAttribute:NSFontAttributeName inRange:range options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
UIFont *currentFont = (UIFont *)value;

if (currentFont == matchFont) {
stop = YES;
wasChanged = YES;
UIFontDescriptor *fontD = [currentFont.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic];
[attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithDescriptor:fontD size:currentFont.pointSize] range:range];
}
}
}];

return wasChanged;
}

@end