NSMutableAttributedString设置字体大小

ste*_*ven 6 macos xcode

我不断遇到的每一个资源都是针对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];
Run Code Online (Sandbox Code Playgroud)

Mic*_*ann 11

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

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];
Run Code Online (Sandbox Code Playgroud)

  • 如果你想为iOS做:将NSFont改为UIFont (2认同)