Cocoa NSAttributedStringKey.font不工作

Cocoa NSAttributedStringKey.font不工作,cocoa,Cocoa,我想向NSTextView中的文本添加临时属性。它适用于NSAttributedStringKey.foregroundColor,但不适用于NSAttributedStringKey.font。同样令人遗憾的是,我无法访问最终标题以查看NSAttributedStringKey的定义位置。(我试着点击cmd,但没有实际的定义,这是一条死胡同) 下面的代码应该可以工作,但字体不变 import Cocoa @NSApplicationMain class AppDelegate: NSObje

我想向NSTextView中的文本添加临时属性。它适用于
NSAttributedStringKey.foregroundColor
,但不适用于
NSAttributedStringKey.font
。同样令人遗憾的是,我无法访问最终标题以查看
NSAttributedStringKey
的定义位置。(我试着点击cmd,但没有实际的定义,这是一条死胡同)

下面的代码应该可以工作,但字体不变

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var textView: NSTextView!

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        textView.layoutManager?.delegate = self
    }
}

extension AppDelegate: NSLayoutManagerDelegate {
    func layoutManager(_ layoutManager: NSLayoutManager,
                       shouldUseTemporaryAttributes attrs: [NSAttributedStringKey : Any] = [:],
                       forDrawingToScreen toScreen: Bool,
                       atCharacterIndex charIndex: Int,
                       effectiveRange effectiveCharRange: NSRangePointer?) -> [NSAttributedStringKey : Any]?
    {
        var attributes = attrs
        if let font = NSFont(name: "Comic Sans MS", size: 14) {
            attributes[NSAttributedStringKey.font] = font
        }
        return attributes
    }
}

addTemporaryAttributes
的文档中可以看到:“目前识别的唯一临时属性是那些不影响布局的属性(颜色、下划线等)。”哦,非常感谢,我错过了。我想我必须解决这个问题。