Check for semibold base font before changing bold text to heavy

This commit is contained in:
Nate Weaver 2020-04-08 16:42:13 -05:00
parent 57a6561733
commit a6411c7afa
1 changed files with 15 additions and 9 deletions

View File

@ -20,25 +20,31 @@ extension NSAttributedString {
let size = baseFont.pointSize let size = baseFont.pointSize
let baseDescriptor = baseFont.fontDescriptor let baseDescriptor = baseFont.fontDescriptor
let traits = baseDescriptor.symbolicTraits let symbolicTraits = baseDescriptor.symbolicTraits
mutable.enumerateAttribute(.font, in: fullRange, options: []) { (font: Any?, range: NSRange, stop: UnsafeMutablePointer<ObjCBool>) in mutable.enumerateAttribute(.font, in: fullRange, options: []) { (font: Any?, range: NSRange, stop: UnsafeMutablePointer<ObjCBool>) in
guard let font = font as? NSFont else { return } guard let font = font as? NSFont else { return }
var newTraits = traits var newSymbolicTraits = symbolicTraits
if font.fontDescriptor.symbolicTraits.contains(.italic) { if font.fontDescriptor.symbolicTraits.contains(.italic) {
newTraits.insert(.italic) newSymbolicTraits.insert(.italic)
} }
var descriptor = baseDescriptor.withSymbolicTraits(newTraits) var descriptor = baseDescriptor.withSymbolicTraits(newSymbolicTraits)
if font.fontDescriptor.symbolicTraits.contains(.bold) { if font.fontDescriptor.symbolicTraits.contains(.bold) {
// This currently assumes we're modifying the title field, which is let baseTraits = baseDescriptor.object(forKey: .traits) as! [NSFontDescriptor.TraitKey: Any]
// already semibold. let baseWeight = baseTraits[.weight] as! NSFont.Weight
let traits: [NSFontDescriptor.TraitKey: Any] = [.weight: NSFont.Weight.heavy]
let attributes: [NSFontDescriptor.AttributeName: Any] = [.traits: traits] // If the base font is semibold (as timeline titles are), make the "bold"
descriptor = descriptor.addingAttributes(attributes) // text heavy for better contrast.
if baseWeight == .semibold {
let traits: [NSFontDescriptor.TraitKey: Any] = [.weight: NSFont.Weight.heavy]
let attributes: [NSFontDescriptor.AttributeName: Any] = [.traits: traits]
descriptor = descriptor.addingAttributes(attributes)
}
} }
let newFont = NSFont(descriptor: descriptor, size: size) let newFont = NSFont(descriptor: descriptor, size: size)