[[NSFontManager sharedFontManager] addFontTrait:(nullable id)sender]
我想实现与仅使用我的代码相同的事情
文档状态
This action method causes the receiver to send its action
message up the responder chain.
By default, the action message is changeFont:.
When a responder replies by providing a font to convert in
a convertFont: message, the receiver converts the font by
adding the trait specified by sender. This trait is determined
by sending a tag message to sender and interpreting it as a font
trait mask for a convertFont:toHaveTrait: message.
所以我尝试了
NSFontManager * fm = [NSFontManager sharedFontManager];
NSFont * font = [fm selectedFont];
NSFont * modifiedFont = [fm convertFont:font toHaveTrait:NSFontItalicTrait];
[fm setSelectedFont:modifiedFont isMultiple:YES];
[NSApp sendAction:@selector(changeFont:) to:fm.target from:fm];
或者
[[self.window firstResponder] tryToPerform:@selector(changeFont:) with:modifiedFont];
但显然我错过了一些观点
NSFontManager 中的实际实现如何更改 NSTextField 上的字体以及如何重现