Revert setting bold to heavy if the base font is semibold

- It was causing conflicts with nested bold + other styles.
- We don't want bold in titles anyway.
This commit is contained in:
Nate Weaver 2020-04-30 17:09:22 -05:00
parent 406a8e0cc0
commit 5d8604714c

View File

@ -36,7 +36,6 @@ extension NSAttributedString {
/// - baseFont: The font to add.
/// - color: The color to add.
func adding(font baseFont: Font, color: Color? = nil) -> NSAttributedString {
let mutable = self.mutableCopy() as! NSMutableAttributedString
let fullRange = NSRange(location: 0, length: mutable.length)
@ -48,37 +47,18 @@ extension NSAttributedString {
let baseDescriptor = baseFont.fontDescriptor
let baseSymbolicTraits = baseDescriptor.symbolicTraits
let baseTraits = baseDescriptor.object(forKey: .traits) as! [FontDescriptor.TraitKey: Any]
let baseWeight = baseTraits[.weight] as! Font.Weight
mutable.enumerateAttribute(.font, in: fullRange, options: []) { (font: Any?, range: NSRange, stop: UnsafeMutablePointer<ObjCBool>) in
guard let font = font as? Font else { return }
var newSymbolicTraits = baseSymbolicTraits
let symbolicTraits = font.fontDescriptor.symbolicTraits
newSymbolicTraits.insert(symbolicTraits)
var descriptor = baseDescriptor.addingAttributes(font.fontDescriptor.fontAttributes)
let newSymbolicTraits = baseSymbolicTraits.union(symbolicTraits)
#if canImport(AppKit)
descriptor = descriptor.withSymbolicTraits(newSymbolicTraits)
let descriptor = baseDescriptor.withSymbolicTraits(newSymbolicTraits)
#else
descriptor = descriptor.withSymbolicTraits(newSymbolicTraits)!
let descriptor = baseDescriptor.withSymbolicTraits(newSymbolicTraits)!
#endif
if symbolicTraits.contains(boldTrait) {
// If the base font is semibold (as timeline titles are), make the "bold"
// text heavy for better contrast.
if baseWeight == .semibold {
let traits: [FontDescriptor.TraitKey: Any] = [.weight: Font.Weight.heavy]
let attributes: [FontDescriptor.AttributeName: Any] = [.traits: traits]
descriptor = descriptor.addingAttributes(attributes)
}
}
let newFont = Font(descriptor: descriptor, size: size)
mutable.addAttribute(.font, value: newFont as Any, range: range)