Since you use the attribute string for the button name, the attributes in this string are responsible for adjusting the alignment.
To center this attribute string, add the NSParagraphStyleAttributeName attribute with a centered 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 above code, Ive created a single attrs dictionary containing all the attributes of an attribute string. From your code, it seems the font color should apply to the whole line.
user557219
source share