[button setAlignment:NSCenterTextAlignment];
,使文本顯示在按鈕的中心。
它工作。
但是,如果我的代碼,按鈕「參考setAlignment」之前設置按鈕title屬性將無法正常工作
- (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'.
}
標題對齊始終顯示爲「NSLeftTextAlignment」而不是「NSCenterTextAlignment」。
歡迎任何評論
如果標題沒有歸因,它會起作用嗎?也許你需要在屬性字符串中設置對齊方式。 – 2011-09-22 11:46:28
它的工作原理是如果標題不屬於,但我不知道如何將對齊添加到屬性字符串 – monsabre
我編輯了問題 – monsabre