无法更改 NSButton 字体

发布于 2025-01-02 18:07:01 字数 432 浏览 2 评论 0原文

我正在尝试更改我创建的 NSButton 子类的字体。当我使用以下代码设置实际按钮时,我可以设置字体:

    [button setFont:[NSFont fontWithName:@"Courier" size:15]];

但是,当我稍后尝试在应用程序中执行此操作时,它不起作用。

我试图让用户选择一种新字体;一旦他们完成此操作,我想更新此按钮以使用所选字体。

我知道我的字体选择过程不是问题,因为我可以将其他 UI 项目的字体更改为用户选择的字体。

此外,我认为问题是由于我的子类正在经历 CABasicAnimation,但是当我删除动画时,它仍然不起作用。此外,我什至可以在动画运行时更改按钮的字体颜色。

最后,我确定我的插座连接正确。

那么问题可能出在哪里呢?过去有人遇到过类似的问题吗?

I'm trying to change the font of a NSButton subclass I've created. I'm able to set the font up when I set up the the actual button using the following code:

    [button setFont:[NSFont fontWithName:@"Courier" size:15]];

However, when I'm trying to do it later on in my application, it doesn't work.

I'm trying to get the user to select a new font; once they've done this, I want to update this button to use the selected font.

I know my font-selection process isn't the issue, as I can change the font of other UI items to what the user's chosen.

Additionally, I thought the problem was due to the fact that my subclass is going through a CABasicAnimation, but when I remove animations, it still doesn't work. Furthermore, I can even change the button's font color while the animation is running.

Finally, I'm certain my outlets are connected right.

So where could the problem be coming from? Has anyone experienced a similar issue in the past?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2025-01-09 18:07:01

我建议您检查 IB 连接,因为 setFont: 应该可以工作。

或者您可以尝试使用:

(void)setAttributedTitle:(NSAttributedString *)aString

其中 NSAttributedString 可以创建自:

- (id)initWithString:(NSString *)aString attributes:(NSDictionary *)attributes

其中属性可以创建为:

NSDictionary * attributes = [NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Courier" size:15] forKey:NSFontAttributeName];

I would recommend you check your IB connections cause setFont: should work.

Or you could try using:

(void)setAttributedTitle:(NSAttributedString *)aString

where an NSAttributedString can be created from:

- (id)initWithString:(NSString *)aString attributes:(NSDictionary *)attributes

where attributes can be created as:

NSDictionary * attributes = [NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Courier" size:15] forKey:NSFontAttributeName];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文