Fix AttributedStringConverter producing non-Dynamic Type fonts
This commit is contained in:
parent
e01664565f
commit
444a47327c
|
@ -357,8 +357,14 @@ public class AttributedStringConverter<Callbacks: HTMLConversionCallbacks> {
|
|||
descriptor = italic
|
||||
}
|
||||
let font = PlatformFont(descriptor: descriptor, size: 0)
|
||||
#if os(iOS) || os(visionOS)
|
||||
let scaled = configuration.fontMetrics.scaledFont(for: font)
|
||||
fontCache[traits] = scaled
|
||||
return scaled
|
||||
#else
|
||||
fontCache[traits] = font
|
||||
return font
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -366,6 +372,7 @@ public struct AttributedStringConverterConfiguration {
|
|||
#if os(iOS) || os(visionOS)
|
||||
public var font: UIFont
|
||||
public var monospaceFont: UIFont
|
||||
public var fontMetrics: UIFontMetrics
|
||||
public var color: UIColor
|
||||
#elseif os(macOS)
|
||||
public var font: NSFont
|
||||
|
@ -375,9 +382,10 @@ public struct AttributedStringConverterConfiguration {
|
|||
public var paragraphStyle: NSParagraphStyle
|
||||
|
||||
#if os(iOS) || os(visionOS)
|
||||
public init(font: UIFont, monospaceFont: UIFont, color: UIColor, paragraphStyle: NSParagraphStyle) {
|
||||
public init(font: UIFont, monospaceFont: UIFont, fontMetrics: UIFontMetrics, color: UIColor, paragraphStyle: NSParagraphStyle) {
|
||||
self.font = font
|
||||
self.monospaceFont = monospaceFont
|
||||
self.fontMetrics = fontMetrics
|
||||
self.color = color
|
||||
self.paragraphStyle = paragraphStyle
|
||||
}
|
||||
|
|
|
@ -44,12 +44,22 @@ final class AttributedStringConverterTests: XCTestCase {
|
|||
}
|
||||
|
||||
private func convert<Callbacks: HTMLConversionCallbacks>(_ html: String, callbacks _: Callbacks.Type = Callbacks.self) -> NSAttributedString {
|
||||
#if os(iOS) || os(visionOS)
|
||||
let config = AttributedStringConverterConfiguration(
|
||||
font: font,
|
||||
monospaceFont: monospaceFont,
|
||||
fontMetrics: .default,
|
||||
color: color,
|
||||
paragraphStyle: .default
|
||||
)
|
||||
#else
|
||||
let config = AttributedStringConverterConfiguration(
|
||||
font: font,
|
||||
monospaceFont: monospaceFont,
|
||||
color: color,
|
||||
paragraphStyle: .default
|
||||
)
|
||||
#endif
|
||||
let converter = AttributedStringConverter<Callbacks>(configuration: config)
|
||||
return converter.convert(html: html)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue