Reputation: 5268
I am using TTTAttributedLabel in my project . And am trying to apply custom font for that label .
#define DEFAULT_FONT(s) [UIFont fontWithName:MY_FONT_NAME size:s]
I used the below code to set font :
@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);
But in my app am font is not getting applied . I need "Welcome" text in black color and remaining part of the text to be in red color . But for my label i need to apply the my custom font .
Upvotes: 1
Views: 2234
Reputation: 17043
Here
if (boldFont) {
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)RGB(134.0, 0, 0).CGColor range:colorRange];
CFRelease(boldFont);
}
you are checking boldFont
and then add a color. Is it correct? If you need o add a font use kCTFontAttributeName
key.
Upvotes: 0
Reputation: 5182
Try this,
CTFontRef boldFont = CTFontCreateWithName((__bridge CFStringRef)systemBoldFont.fontName, systemBoldFont.pointSize, NULL);
if (boldFont) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)boldFont range:nameRange];
CFRelease(boldFont);
}
Upvotes: 2