我假设您有连接到 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];
编辑:
我刚刚意识到您也在询问如何从字体面板获取新字体。
要从字体面板获取新字体,您的代码实际上应该可以正常工作,除非从未初始化“字体”(旧字体)。如果字体为 null,则 [sender convertFont:font] 将返回 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);
}