-(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];
}
![]()
![]()
![]()