我只想使用我的代码实现与[[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上的字体,以及如何再现
发布于 2019-11-22 09:27:50
这是一种有点烦人的方法。字体管理器检查发送者的标签,因此只需要匹配窗口的Edit菜单中的标记
let bold = NSMenuItem()
bold.tag = 2
NSFontManager.shared.addFontTrait(bold)这也适用于NSButton,或者可能是任何带有标记的控件。这也意味着不需要编写一行代码就可以做到这一点。只需在情节提要中添加一个按钮,在场景中添加NSFontManager作为对象即可。连接字体管理器对象按钮,然后选择addFontTrait:
我还没想出怎么用这种方法来消除胆量。“窗口”菜单能够做到这一点。
https://stackoverflow.com/questions/38857904
复制相似问题