Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSButton setAlignment does not work

Tags:

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