我在我的项目中使用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);
但是在我的应用程序中,没有应用字体。我需要黑色的“ Welcome”文本,其余的部分需要红色。但是对于我的标签,我需要应用我的自定义字体。
尝试这个,
CTFontRef boldFont = CTFontCreateWithName((__bridge CFStringRef)systemBoldFont.fontName, systemBoldFont.pointSize, NULL);
if (boldFont) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)boldFont range:nameRange];
CFRelease(boldFont);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句