本文介绍了NSPopButton与字体系列列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

如何创建一个NSPopupButton列表的字体系列,其中每个项目是自己的字体,如附件截图所示。我想使用绑定来实现这一点。

How can I create a NSPopupButton with a list of font families where each item is in its own font as shown in the attached screenshot. I want to use bindings to achieve this.

我可以通过将NSPopupButton内容绑定到 [[NSFontManager sharedFontManager] availableFontFamilies]返回的值来填充NSPopupButton 但我不知道如何以自己的字体得到每一行。

I am able to populate the NSPopupButton by binding NSPopupButton content to value returned by [[NSFontManager sharedFontManager] availableFontFamilies] but I can't figure out how to get each individual row in its own font?

推荐答案

我不知道我能做到,

// fontPopup1 is an instance of NSPopupMenu
NSMenu *menu = [[NSMenu alloc] init];
NSArray *familyNames = [[NSFontManager sharedFontManager] availableFontFamilies];
NSMutableArray *fontArray = [[NSMutableArray alloc] initWithObjects:nil];
for (NSString *family in familyNames) {
    [fontArray addObject:family];
}
for (NSInteger i2 = 0; i2 < fontArray.count; i2++) {
    NSString *family = [fontArray objectAtIndex:i2];
    NSMutableAttributedString *attrStr =[[NSMutableAttributedString alloc]initWithString:family];
    CGFloat fontSize = [NSFont systemFontSize];
    [attrStr addAttribute:NSFontAttributeName value:[NSFont fontWithName:family size:fontSize] range:NSMakeRange(0,family.length)];
    NSMenuItem *menuItem = [[NSMenuItem alloc] init];
    [menuItem setAttributedTitle:attrStr];
    [menu addItem:menuItem];
}
[fontPopup1 setMenu:menu];

这篇关于NSPopButton与字体系列列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 11:32