首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSFontManager addFontTrait如何更改UI元素上的实际字体

NSFontManager addFontTrait如何更改UI元素上的实际字体
EN

Stack Overflow用户
提问于 2016-08-09 18:25:01
回答 1查看 418关注 0票数 7

我只想使用我的代码实现与[[NSFontManager sharedFontManager] addFontTrait:(nullable id)sender]相同的功能

医生说

代码语言:javascript
复制
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.

所以我试着

代码语言:javascript
复制
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];

代码语言:javascript
复制
[[self.window firstResponder] tryToPerform:@selector(changeFont:) with:modifiedFont];

但很明显我遗漏了一点

NSFontManager中的实际实现如何更改NSTextField上的字体,以及如何再现

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-22 09:27:50

这是一种有点烦人的方法。字体管理器检查发送者的标签,因此只需要匹配窗口的Edit菜单中的标记

代码语言:javascript
复制
let bold = NSMenuItem()
bold.tag = 2
NSFontManager.shared.addFontTrait(bold)

这也适用于NSButton,或者可能是任何带有标记的控件。这也意味着不需要编写一行代码就可以做到这一点。只需在情节提要中添加一个按钮,在场景中添加NSFontManager作为对象即可。连接字体管理器对象按钮,然后选择addFontTrait:

我还没想出怎么用这种方法来消除胆量。“窗口”菜单能够做到这一点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38857904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档