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