gpt4 book ai didi

xcode - NSMutableAttributedString设置字体大小

转载 作者:行者123 更新时间:2023-12-04 13:50:38 25 4
gpt4 key购买 nike

我为此遇到的所有资源都适用于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方法从一开始就设置字体属性:

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

关于xcode - NSMutableAttributedString设置字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21687349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com