steven
steven

Reputation: 698

NSMutableAttributedString set font size

Every resource I keep coming across for this is for iOS. I can't seem to get this working properly for Mac OSX. Any idea how to get this to set the font size as well? It currently sets the color and center alignment properly. Thanks

NSMutableAttributedString *libTitle = [[NSMutableAttributedString alloc] initWithString:@"Library"];
[libTitle addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:NSMakeRange(0,[@"Library" length] ) ];
[libTitle setAlignment:NSCenterTextAlignment range:NSMakeRange(0, [@"Library" length])];
[libTitle addAttribute:NSFontSizeAttribute value:[NSFont systemFontOfSize:18.0] range:NSMakeRange(0, [@"Library" length])];

[self.libbtn setAttributedTitle:libTitle];

Upvotes: 6

Views: 18469

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89569

Try setting your font attributes from the get go, in the init method:

NSFont *systemFont = [NSFont systemFontOfSize:18.0f];
NSDictionary * fontAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:systemFont, NSFontAttributeName, nil];
NSMutableAttributedString *libTitle = [[NSMutableAttributedString alloc] initWithString:@"Library" attributes:fontAttributes];
NSRange rangeOfTitle = NSMakeRange(0,[libTitle length]);
[libTitle addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:rangeOfTitle];
[libTitle setAlignment:NSCenterTextAlignment range:rangeOfTitle];

[self.libbtn setAttributedTitle:libTitle];

Upvotes: 11

Related Questions