我想在面板上设置字体并更改所选字体。我正在使用 NSColorWell 打开并选择颜色。对于字体,我可以使用什么?如何在字体面板关闭时打开字体面板并执行操作?
目前我正在使用
'- (IBAction)Open_Font_Button:(id)sender
{
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[fontManager setDelegate:self];
[fontManager setTarget:self];
[fontManager orderFrontFontPanel:self];
}
- (void)changeFont:(id)sender
{
font = [sender convertFont:font];
NSLog(@"%@", font);
}
'
但在 chnageFont
上,当我更改任何字体或其大小时,它会崩溃。
最佳答案
我假设您已连接 ColorWell 和 textField 的导出:
IBOutlet NSColorWell *colorWell;
IBOutlet NSTextField *textfield;
您应该设置一些有关 NSColorPanel 的内容:
[NSColor setIgnoresAlpha:NO];
[[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
当您打开或关闭可能显示颜色面板的窗口时,您应该确保没有留下一个颜色面板:
if ([NSColorPanel sharedColorPanelExists])
{
[[NSColorPanel sharedColorPanel] close];
}
然后在颜色井的 IBAction 方法中,您可以获得颜色:
NSColor *color;
color = [colorWell color];
然后您可以设置字体和颜色:
[textField setFont:anNSFont *];
[textField setTextColor:color];
编辑:
我刚刚意识到您还询问如何从字体面板获取新字体。
要从字体面板获取新字体,您的代码实际上应该可以正常工作,除非“字体”(旧字体)从未初始化。如果 font 为 null,则 [sender ConvertFont:font] 将返回 null。
这会打印 null:
- (void)changeFont:(id)sender
{
NSFont *font;
font = [sender convertFont:font]; // Reset the font
NSLog(@"%@", font);
}
这会打印字体:
- (void)changeFont:(id)sender
{
NSFont *font = [NSFont fontWithName:@"Helvetica" size:12]; // Initialize the old font
font = [sender convertFont:font]; // Reset the font
NSLog(@"%@", font);
}
关于macos - 如何在 cocoa 中设置 nstextfield 的字体和颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27741829/