Skip to content

Commit

Permalink
Allow text to be both bold and italic (SimonFairbairn#150).
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanPartzsch committed Mar 7, 2024
1 parent b3a5c14 commit 770ab52
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
24 changes: 20 additions & 4 deletions Sources/SwiftyMarkdown/SwiftyMarkdown+iOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ extension SwiftyMarkdown {
fontName = italic.fontName ?? fontName
fontSize = italic.fontSize
globalItalic = true
case .boldItalic:
globalBold = true
globalItalic = true
case .strikethrough:
fontName = strikethrough.fontName ?? fontName
fontSize = strikethrough.fontSize
Expand Down Expand Up @@ -139,11 +142,24 @@ extension SwiftyMarkdown {
font = UIFont.preferredFont(forTextStyle: textStyle)
}

if globalItalic, let italicDescriptor = font.fontDescriptor.withSymbolicTraits(.traitItalic) {
font = UIFont(descriptor: italicDescriptor, size: fontSize ?? 0)
// if globalItalic, let italicDescriptor = font.fontDescriptor.withSymbolicTraits(.traitItalic) {
// font = UIFont(descriptor: italicDescriptor, size: fontSize ?? 0)
// }
// if globalBold, let boldDescriptor = font.fontDescriptor.withSymbolicTraits(.traitBold) {
// font = UIFont(descriptor: boldDescriptor, size: fontSize ?? 0)
// }

var traits: UIFontDescriptor.SymbolicTraits = []
if globalItalic {
traits.insert(.traitItalic)
}
if globalBold {
traits.insert(.traitBold)
}
if globalBold, let boldDescriptor = font.fontDescriptor.withSymbolicTraits(.traitBold) {
font = UIFont(descriptor: boldDescriptor, size: fontSize ?? 0)
if !traits.isEmpty, let descriptor = font.fontDescriptor.withSymbolicTraits(traits) {
let customFont = UIFont(descriptor: descriptor, size: fontSize ?? 0)
let fontMetrics = UIFontMetrics(forTextStyle: textStyle)
font = fontMetrics.scaledFont(for: customFont)
}

return font
Expand Down
30 changes: 17 additions & 13 deletions Sources/SwiftyMarkdown/SwiftyMarkdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public enum CharacterStyle : CharacterStyling {
case none
case bold
case italic
case boldItalic
case code
case link
case image
Expand Down Expand Up @@ -557,20 +558,23 @@ extension SwiftyMarkdown {
attributes[.paragraphStyle] = paragraphStyle


for token in finalTokens {
attributes[.font] = self.font(for: line)
attributes[.link] = nil
attributes[.strikethroughStyle] = nil
attributes[.foregroundColor] = self.color(for: line)
for token in finalTokens {
attributes[.font] = self.font(for: line)
attributes[.link] = nil
attributes[.strikethroughStyle] = nil
attributes[.foregroundColor] = self.color(for: line)
attributes[.underlineStyle] = nil
guard let styles = token.characterStyles as? [CharacterStyle] else {
continue
}
if styles.contains(.italic) {
attributes[.font] = self.font(for: line, characterOverride: .italic)
attributes[.foregroundColor] = self.italic.color
}
if styles.contains(.bold) {
guard let styles = token.characterStyles as? [CharacterStyle] else {
continue
}
if styles.contains(.boldItalic) {
attributes[.font] = self.font(for: line, characterOverride: .boldItalic)
} else if styles.contains(.italic), styles.contains(.bold) {
attributes[.font] = self.font(for: line, characterOverride: .boldItalic)
} else if styles.contains(.italic) {
attributes[.font] = self.font(for: line, characterOverride: .italic)
attributes[.foregroundColor] = self.italic.color
} else if styles.contains(.bold) {
attributes[.font] = self.font(for: line, characterOverride: .bold)
attributes[.foregroundColor] = self.bold.color
}
Expand Down

0 comments on commit 770ab52

Please sign in to comment.