gpt4 book ai didi

ios - 如何将自定义字体应用于 TTTAttributedLabel

转载 作者:行者123 更新时间:2023-11-29 10:45:30 25 4
gpt4 key购买 nike

我正在使用 TTTAttributedLabel在我的项目中。并且正在尝试为该标签应用自定义字体。

#define DEFAULT_FONT(s)     [UIFont fontWithName:MY_FONT_NAME size:s]

我使用下面的代码来设置字体:

@property(nonatomic, strong) TTTAttributedLabel *welcomeMessage;

NSString *welcomeMessageString = [[NSString stringWithFormat:@"%@",[self.dashboardViewModel getWelcomeMessage]] uppercaseString];
[self.welcomeMessage setText:welcomeMessageString afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString){
NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"Welcome " options:NSCaseInsensitiveSearch];

NSRange colorRange = NSMakeRange((boldRange.location + boldRange.length), (welcomeMessageString.length - boldRange.length));
UIFont *systemBoldFont = DEFAULT_FONT(13);
CTFontRef boldFont = CTFontCreateWithName((__bridge CFStringRef)systemBoldFont.fontName, systemBoldFont.pointSize, NULL);
if (boldFont) {
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)RGB(134.0, 0, 0).CGColor range:colorRange];
CFRelease(boldFont);

}


return mutableAttributedString;
}];
self.welcomeMessage.font = DEFAULT_FONT(13);

但在我的应用程序中没有应用 am 字体。我需要黑色的“欢迎”文字,其余部分文字为红色。但是对于我的标签,我需要应用我的自定义字体。

最佳答案

试试这个,

 CTFontRef boldFont = CTFontCreateWithName((__bridge CFStringRef)systemBoldFont.fontName, systemBoldFont.pointSize, NULL);
if (boldFont) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)boldFont range:nameRange];
CFRelease(boldFont);

}

关于ios - 如何将自定义字体应用于 TTTAttributedLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22554195/

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