简体   繁体   English

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

[英]How to apply custom font to TTTAttributedLabel

I am using TTTAttributedLabel in my project . 我在我的项目中使用TTTAttributedLabel 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 . 我需要黑色的“ Welcome”文本,其余的部分需要红色。 But for my label i need to apply the my custom font . 但是对于我的标签,我需要应用我的自定义字体。

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

    }

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. 您正在检查boldFont ,然后添加颜色。 Is it correct? 这是正确的吗? If you need o add a font use kCTFontAttributeName key. 如果需要o添加字体,请使用kCTFontAttributeName键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TTTAttributedLabel自定义addLinkToURL不起作用 - TTTAttributedLabel custom addLinkToURL not working 如何将TTTAttributedLabel与'@'和'#'一起使用? - How to use TTTAttributedLabel with '@' and '#'? 如何在自定义 UIbutton 和 swift 处应用自定义字体? - How do I apply custom font at custom UIbutton with swift? 当UIAlertView呈现时,TTTAttributedLabel链接字体更改 - TTTAttributedLabel link font changing when UIAlertView presents 如何将自定义字体系列应用于您的flutter应用程序? - How to apply Custom font family to your flutter app? 如何使TTTAttributedLabel的truncationTokenString成为链接? - How to make truncationTokenString of TTTAttributedLabel a link? 如何在通知内容扩展中为我的自定义视图应用自定义字体/颜色和半透明背景? - How to apply custom font/color and semi-transparent background for my custom view in notification content extension? 如何在TTTAttributedLabel中将HTML锚点设为可点击链接? - How to make an HTML anchor as clickable link in TTTAttributedLabel? 如何在IOS中以编程方式创建TTTAttributedLabel对齐中心 - How to make TTTAttributedLabel align center programatically in IOS 如何将粗体和斜体字体应用于NSAttributedString? - How to apply both bold and italic font to an NSAttributedString?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM