From 770ab52f4419ca693c3274c425dd7a0452ff07be Mon Sep 17 00:00:00 2001 From: Stephan Partzsch Date: Thu, 7 Mar 2024 12:06:42 +0100 Subject: [PATCH] Allow text to be both bold and italic (https://github.com/SimonFairbairn/SwiftyMarkdown/pull/150). Done by https://github.com/gsbernstein --- .../SwiftyMarkdown/SwiftyMarkdown+iOS.swift | 24 ++++++++++++--- Sources/SwiftyMarkdown/SwiftyMarkdown.swift | 30 +++++++++++-------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/Sources/SwiftyMarkdown/SwiftyMarkdown+iOS.swift b/Sources/SwiftyMarkdown/SwiftyMarkdown+iOS.swift index a2e4fee..d867409 100644 --- a/Sources/SwiftyMarkdown/SwiftyMarkdown+iOS.swift +++ b/Sources/SwiftyMarkdown/SwiftyMarkdown+iOS.swift @@ -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 @@ -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 diff --git a/Sources/SwiftyMarkdown/SwiftyMarkdown.swift b/Sources/SwiftyMarkdown/SwiftyMarkdown.swift index 4436d43..f216e2d 100644 --- a/Sources/SwiftyMarkdown/SwiftyMarkdown.swift +++ b/Sources/SwiftyMarkdown/SwiftyMarkdown.swift @@ -21,6 +21,7 @@ public enum CharacterStyle : CharacterStyling { case none case bold case italic + case boldItalic case code case link case image @@ -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 }