NSButton setAlignment不工作 [英] NSButton setAlignment does not work
问题描述
我使用了
[button setAlignment:NSCenterTextAlignment];
使文字显示在按钮的中央。
$
但是如果我在代码之前设置按钮标题属性,按钮'setAlignment'将不起作用。
$ c $ b
- (void)setButtonTitle:(NSButton *)button fontName:(NSString *)fontName fontSize:(CGFloat)fontSize fontColor:
{
NSMutableAttributedString * attributingString =
[NSMutableAttributedString alloc] initWithString:[button title]
attributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:fontName size:fontSize ]
forKey:NSFontAttributeName]];
[attributString addAttribute:NSForegroundColorAttributeName
value:fontColor
range:NSMakeRange(0,[[button title] length])];
[button setAttributedTitle:attributString];
[button setAlignment:NSCenterTextAlignment]; //按钮标题对齐总是显示为'NSLeftTextAlignment',而不是'NSCenterTextAlignment'。
}
标题对齐总是显示为NSLeftTextAlignment,而不是NSCenterTextAlignment '。
欢迎使用任何注释
要将该属性字符串置于中心,请添加 NSParagraphStyleAttributeName
具有中心对齐值的属性:
NSMutableParagraphStyle * centredStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[centredStyle setAlignment:NSCenterTextAlignment];
NSDictionary * attrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
NSParagraphStyleAttributeName,
[NSFont fontWithName:fontName size:fontSize],
NSFontAttributeName,
fontColor,
NSForegroundColorAttributeName,
nil];
NSMutableAttributedString * attributingString =
[[NSMutableAttributedString alloc] initWithString:[button title]
attributes:attrs];
[button setAttributedTitle:attributString];
在上面的代码中,我创建了一个 attrs
包含属性字符串的所有属性的字典。从您的代码,看起来像字体颜色应该应用于整个字符串。
I used
[button setAlignment:NSCenterTextAlignment];
to make the text display at the center of the button.
It worked.
But if I set button title attribute before the code, button 'setAlignment' will not work
- (void)setButtonTitle:(NSButton*)button fontName:(NSString*)fontName fontSize:(CGFloat)fontSize fontColor:(NSColor*)fontColor;
{
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:[button title]
attributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:fontName size:fontSize]
forKey:NSFontAttributeName]];
[attributedString addAttribute:NSForegroundColorAttributeName
value:fontColor
range:NSMakeRange(0, [[button title] length] )];
[button setAttributedTitle: attributedString];
[button setAlignment:NSCenterTextAlignment];//button title alignment always displayed as 'NSLeftTextAlignment' rather than 'NSCenterTextAlignment'.
}
title alignment always displayed as 'NSLeftTextAlignment' rather than 'NSCenterTextAlignment'.
Welcome any comment
Since you’re using an attributed string for the button title, the attributes in that string are responsible for setting the alignment.
To centre that attributed string, add an NSParagraphStyleAttributeName
attribute with a centred alignment value:
NSMutableParagraphStyle *centredStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[centredStyle setAlignment:NSCenterTextAlignment];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle,
NSParagraphStyleAttributeName,
[NSFont fontWithName:fontName size:fontSize],
NSFontAttributeName,
fontColor,
NSForegroundColorAttributeName,
nil];
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:[button title]
attributes:attrs];
[button setAttributedTitle: attributedString];
In the code above, I’ve created a single attrs
dictionary holding all the attributes for the attributed string. From your code, it looks like the font colour should be applied to the whole string anyway.
这篇关于NSButton setAlignment不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!