可以为 NSTextField 设置对齐方式或字体,但不能同时设置两者

发布于 2024-12-14 03:27:16 字数 1648 浏览 2 评论 0原文

我有一个面板笔尖,其文本字段之一有一个出口,该文本字段在笔尖中设置为居中对齐。当我显示面板时,我希望该文本字段加粗。 由于 NSTextField 是 NSControl 的子类,因此它可以使用 setAttributedStringValue 方法并获取属性字符串。因此,我采用了如下粗体字体:

NSFont *fontBolded = [NSFont fontWithName:@"Baskerville Bold" size:12.0f];
NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObject:fontBolded forKey:NSFontAttributeName];   
NSString *sHelloUser = NSLocalizedString(@"Hello User", @"Hello User");
NSAttributedString *attrsHelloUser = [[NSAttributedString alloc] initWithString: sHelloUser attributes:dictBoldAttr];
[self.fooController.tfPanelCenteredField setAttributedStringValue:attrsHelloUser];  
[attrsHelloUser release];

粗体显示正常,但该字段现在左对齐。

我尝试添加 setAlignment,但没有效果:

[self.fooController.tfPanelCenteredField setAlignment:NSCenterTextAlignment];

所以我尝试向属性字符串的属性添加居中段落样式:

NSFont *fontBolded = [NSFont fontWithName:@"Baskerville Bold" size:12.0f];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];   
[paragStyle setAlignment:NSCenterTextAlignment]; 
NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObjectsAndKeys:paragStyle, NSParagraphStyleAttributeName, fontBolded, NSFontNameAttribute, nil];
NSString *sHelloUser = NSLocalizedString(@"Hello User", @"Hello User");
NSAttributedString *attrsHelloUser = [[NSAttributedString alloc] initWithString: sHelloUser attributes:dictBoldAttr];
[self.fooController.tfPanelCenteredField setAttributedStringValue:attrsHelloUser];  
[attrsHelloUser release];
[paragStyle release];

现在文本字段再次居中,但粗体消失了。就好像属性字符串可以接受一个且仅一个属性设置。我错过了一些简单的事情吗?

I have a panel nib with an outlet for one of its textfields, which is set in the nib to have centered alignment. When I display the panel, I would like this textfield to be bolded.
Since NSTextField is a subclass of NSControl, it can use the setAttributedStringValue method and take an attributed string. So I incorporated a bold font like this:

NSFont *fontBolded = [NSFont fontWithName:@"Baskerville Bold" size:12.0f];
NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObject:fontBolded forKey:NSFontAttributeName];   
NSString *sHelloUser = NSLocalizedString(@"Hello User", @"Hello User");
NSAttributedString *attrsHelloUser = [[NSAttributedString alloc] initWithString: sHelloUser attributes:dictBoldAttr];
[self.fooController.tfPanelCenteredField setAttributedStringValue:attrsHelloUser];  
[attrsHelloUser release];

The bolding shows up OK, but the field is now left-aligned.

I tried adding a setAlignment, but it had no effect:

[self.fooController.tfPanelCenteredField setAlignment:NSCenterTextAlignment];

So I tried adding a centered parapraph style to the attributed string’s attributes:

NSFont *fontBolded = [NSFont fontWithName:@"Baskerville Bold" size:12.0f];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];   
[paragStyle setAlignment:NSCenterTextAlignment]; 
NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObjectsAndKeys:paragStyle, NSParagraphStyleAttributeName, fontBolded, NSFontNameAttribute, nil];
NSString *sHelloUser = NSLocalizedString(@"Hello User", @"Hello User");
NSAttributedString *attrsHelloUser = [[NSAttributedString alloc] initWithString: sHelloUser attributes:dictBoldAttr];
[self.fooController.tfPanelCenteredField setAttributedStringValue:attrsHelloUser];  
[attrsHelloUser release];
[paragStyle release];

Now the textfield is centered again, but the bolding is gone. It’s as though the attributed string can accept one and only one attribute setting. Am I missing something simple?

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

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

发布评论

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

评论(1

迎风吟唱 2024-12-21 03:27:16

您的代码中有一个拼写错误。 NSFontNameAttribute 应该是 NSFontAttributeName

所以你的属性字典是:

    NSFont *fontBolded = [NSFont fontWithName:@"Baskerville Bold" size:12.0f];
    NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];   
    [paragStyle setAlignment:NSCenterTextAlignment]; 
    NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObjectsAndKeys:
                                  fontBolded, NSFontAttributeName,
                                  paragStyle, NSParagraphStyleAttributeName,
                                  nil];

You have a typo in your code. NSFontNameAttribute should be NSFontAttributeName.

So your attributes dictionary is:

    NSFont *fontBolded = [NSFont fontWithName:@"Baskerville Bold" size:12.0f];
    NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];   
    [paragStyle setAlignment:NSCenterTextAlignment]; 
    NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObjectsAndKeys:
                                  fontBolded, NSFontAttributeName,
                                  paragStyle, NSParagraphStyleAttributeName,
                                  nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文