xcode – NSMutableAttributedString设置字体大小

前端之家收集整理的这篇文章主要介绍了xcode – NSMutableAttributedString设置字体大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我不断遇到的每一个资源都是针对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];

解决方法

尝试在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];
原文链接:https://www.f2er.com/iOS/328246.html

猜你在找的iOS相关文章