Keep monospace fonts monospaced; use a variable for the current symbolic traits

This commit is contained in:
Nate Weaver 2020-04-10 15:53:11 -05:00
parent db166ba0d7
commit 2a00b4a4a9
1 changed files with 10 additions and 4 deletions

View File

@ -20,7 +20,7 @@ extension NSAttributedString {
let size = baseFont.pointSize
let baseDescriptor = baseFont.fontDescriptor
let symbolicTraits = baseDescriptor.symbolicTraits
let baseSymbolicTraits = baseDescriptor.symbolicTraits
let baseTraits = baseDescriptor.object(forKey: .traits) as! [NSFontDescriptor.TraitKey: Any]
let baseWeight = baseTraits[.weight] as! NSFont.Weight
@ -28,15 +28,21 @@ extension NSAttributedString {
mutable.enumerateAttribute(.font, in: fullRange, options: []) { (font: Any?, range: NSRange, stop: UnsafeMutablePointer<ObjCBool>) in
guard let font = font as? NSFont else { return }
var newSymbolicTraits = symbolicTraits
var newSymbolicTraits = baseSymbolicTraits
if font.fontDescriptor.symbolicTraits.contains(.italic) {
let symbolicTraits = font.fontDescriptor.symbolicTraits
if symbolicTraits.contains(.italic) {
newSymbolicTraits.insert(.italic)
}
if symbolicTraits.contains(.monoSpace) {
newSymbolicTraits.insert(.monoSpace)
}
var descriptor = baseDescriptor.withSymbolicTraits(newSymbolicTraits)
if font.fontDescriptor.symbolicTraits.contains(.bold) {
if symbolicTraits.contains(.bold) {
// If the base font is semibold (as timeline titles are), make the "bold"
// text heavy for better contrast.