NSMutableAttributedString设置字体大小

时间:2014-02-10 20:35:33

标签: xcode macos

我不断遇到的每一个资源都是针对iOS的。我似乎无法让这个适用于Mac OSX。知道如何设置字体大小吗?它目前正确设置颜色和中心对齐。感谢

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];

1 个答案:

答案 0 :(得分:11)

尝试在init方法中设置字体属性:

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];