Skip to content

Commit

Permalink
Don't alter element font directly
Browse files Browse the repository at this point in the history
  • Loading branch information
gaebel committed Aug 21, 2021
1 parent d95f931 commit 4270b02
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 7 additions & 5 deletions MarkdownKit/Sources/Common/Elements/Bold/MarkdownBold.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ open class MarkdownBold: MarkdownCommonElement {
public func match(_ match: NSTextCheckingResult, attributedString: NSMutableAttributedString) {
attributedString.deleteCharacters(in: match.range(at: 4))

attributedString.enumerateAttribute(.font, in: match.range(at: 3)) { value, range, stop in
guard let font = value as? MarkdownFont else { return }
var attributes = self.attributes

attributedString.enumerateAttribute(.font, in: match.range(at: 3)) { value, range, _ in
guard let currentFont = value as? MarkdownFont else { return }
if let customFont = self.font {
self.font = font.isItalic() ? customFont.bold().italic() : customFont.bold()
attributes[.font] = currentFont.isItalic() ? customFont.bold().italic() : customFont.bold()
} else {
attributedString.addAttribute(
NSAttributedString.Key.font,
value: font.bold(),
value: currentFont.bold(),
range: range
)
}
}

addAttributes(attributedString, range: match.range(at: 3))
attributedString.addAttributes(attributes, range: match.range(at: 3))

attributedString.deleteCharacters(in: match.range(at: 2))
}
Expand Down
12 changes: 7 additions & 5 deletions MarkdownKit/Sources/Common/Elements/Italic/MarkdownItalic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ open class MarkdownItalic: MarkdownCommonElement {
public func match(_ match: NSTextCheckingResult, attributedString: NSMutableAttributedString) {
attributedString.deleteCharacters(in: match.range(at: 4))

attributedString.enumerateAttribute(.font, in: match.range(at: 3)) { value, range, stop in
guard let font = value as? MarkdownFont else { return }
var attributes = self.attributes

attributedString.enumerateAttribute(.font, in: match.range(at: 3)) { value, range, _ in
guard let currentFont = value as? MarkdownFont else { return }
if let customFont = self.font {
self.font = font.isBold() ? customFont.bold().italic() : customFont.italic()
attributes[.font] = currentFont.isBold() ? customFont.bold().italic() : customFont.italic()
} else {
attributedString.addAttribute(
NSAttributedString.Key.font,
value: font.italic(),
value: currentFont.italic(),
range: range
)
}
}

addAttributes(attributedString, range: match.range(at: 3))
attributedString.addAttributes(attributes, range: match.range(at: 3))

attributedString.deleteCharacters(in: match.range(at: 2))
}
Expand Down

0 comments on commit 4270b02

Please sign in to comment.