• 一个label两种颜色,一个label两种字体


    -(void)addLabel{
        
        UILabel *label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor grayColor];
        [self.view addSubview:label];
        
        label.translatesAutoresizingMaskIntoConstraints = NO;
        
        NSLayoutConstraint *leftic =[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:20];
        [self.view addConstraint:leftic];
    
        NSLayoutConstraint *rightic =[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:-20];
        [self.view addConstraint:rightic];
    
    
        NSLayoutConstraint *topic =[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:150];
        [self.view addConstraint:topic];
    
        NSLayoutConstraint *heightic =[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:200];
        [label addConstraint:heightic];
        
        
        label.numberOfLines = 0;
    
    //    [self changeColor:label];
    //    [self changeFontLabel:label font:40];
        [self changeColorAndFontLabel:label font:40];
        
    }
    
    //更改字体
    - (void)changeFontLabel:(UILabel *)label font:(int)font
    {
        //label  需要操作的Label
        //font   该字符的字号
        NSMutableAttributedString *noteString = [[NSMutableAttributedString alloc] initWithString:@"照片中信息真实有效且清晰可见,包括手持证件人的五官、身份证上的所有信息(请看三遍再上传图片噢)"];
        NSRange stringRange = NSMakeRange(0, 1); //该字符串的位置
        [noteString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:stringRange];
        [label setAttributedText: noteString];
        
    }
    
    //两种字体,两种颜色。
    - (void)changeColorAndFontLabel:(UILabel *)label font:(int)font
    {
        //label  需要操作的Label
        //font   该字符的字号
        NSMutableAttributedString *noteString = [[NSMutableAttributedString alloc] initWithString:@"照片中信息真实有效且清晰可见,包括手持证件人的五官、身份证上的所有信息(请看三遍再上传图片噢)"];
        NSRange stringRange = NSMakeRange(0, 1); //该字符串的位置
        [noteString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:stringRange];
        [label setAttributedText: noteString];
        
        //将两个写在同一个方法里可以同时实现:一个label两种字体,一个label两种颜色
        //但是分开执行两个方法,后面执行的,可能把前面执行的覆盖掉。
        NSRange redRange = NSMakeRange([[noteString string] rangeOfString:@"(请看三遍再上传图片噢)"].location, [[noteString string] rangeOfString:@"(请看三遍再上传图片噢)"].length);
        //需要设置的位置
        [noteString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
        
        //设置颜色
        [label setAttributedText:noteString];
    
    }
    
    //两种颜色
    -(void)changeColor:(UILabel *)label{
        
        //下面更改颜色
        NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:@"照片中信息真实有效且清晰可见,包括手持证件人的五官、身份证上的所有信息(请看三遍再上传图片噢)"];
        NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@"(请看三遍再上传图片噢)"].location, [[noteStr string] rangeOfString:@"(请看三遍再上传图片噢)"].length);
        //需要设置的位置
        [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
        
        //设置颜色
        [label setAttributedText:noteStr];
        
        
    }

  • 相关阅读:
    LOJ#3157. 「NOI2019」机器人 DP+拉格朗日插值
    BZOJ4832 [Lydsy2017年4月月赛]抵制克苏恩 记忆化搜索
    LazySysAdmin 靶机渗透
    zico2靶机渗透
    6_面向对象-下之关键字:static
    5_面向对象-中之单元测试方法、包装类的使用
    5_面向对象-中之Object类的使用
    5_面向对象-中之面向对象的特征三:多态性
    5_面向对象-中之子类对象实例化全过程
    5_面向对象-中之关键字:super
  • 原文地址:https://www.cnblogs.com/OIMM/p/8876502.html
Copyright © 2020-2023  润新知