iOS - 富文本直接设置文字的字体大小和颜色

本教程将指导你使用SwiftUI构建iOS应用程序的基础知识,包括界面设计、状态管理和手势识别等关键概念。

 

富文本效果图:

 

富文本实现代码:

UILabel *orderSureLabel = [Common lableFrame:CGRectZero title:@"" backgroundColor:[UIColor clearColor] font:[UIFont systemFontOfSize:15] textColor:[UIColor colorWithHexString:@"#666666"]];
    //    orderSureLabel.backgroundColor = [UIColor yellowColor];
    NSMutableAttributedString *orderSureStr = [Common setupAttributeString:@"在主页面下方找到顺道工单,点击“接单”,确认接单。" rangeText:@"顺道工单" textColor:[UIColor colorWithHexString:@"#F34949"]];
    orderSureLabel.attributedText = orderSureStr;
//    orderSureLabel.text = @"在主页面下方找到顺道工单,点击“接单”,确认接单。";
    orderSureLabel.numberOfLines = 0;
    [self addSubview:orderSureLabel];
    [orderSureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(orderLabel.mas_bottom).offset(30);
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.centerX.equalTo(self);
    }];

 

富文本实现的分类方法:

#pragma mark - 创建UILabel

+ (UILabel*)lableFrame:(CGRect)frame title:(NSString *)title backgroundColor:(UIColor*)color font:(UIFont*)font textColor:(UIColor*)textColor {
    
    UILabel *lable=[[UILabel alloc]initWithFrame:frame];
    
    lable.text=title;
    
    lable.font=font;
    
    [lable setBackgroundColor:color];
    
    lable.textColor=textColor;
    
    return lable;
    
}

#pragma mark - 富文本设置部分字体颜色

+ (NSMutableAttributedString *)setupAttributeString:(NSString *)text rangeText:(NSString *)rangeText textColor:(UIColor *)color{
    
    NSRange hightlightTextRange = [text rangeOfString:rangeText];
    
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
    
    if (hightlightTextRange.length > 0) {
        
        [attributeStr addAttribute:NSForegroundColorAttributeName
                 
                 value:color
                 
                 range:hightlightTextRange];
        
        [attributeStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14.0f] range:hightlightTextRange];
        
        return attributeStr;
        
        }else {
            
            return [rangeText copy];
            
            }
}

 

转载于:https://www.cnblogs.com/gongyuhonglou/p/10517310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值