- (void)setRichNumberWithLabel:(UILabel *)label Color:(UIColor *) color FontSize:(CGFloat)size{ //将Label的text转化为NSMutalbeAttributedString NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:label.text]; //定义空字符串 NSString *temp = nil; //根据属属性字符串的长度循环 for(int i = 0; i < [attributedString length]; i++){ //每次取1个长度的字符串 temp = [label.text substringWithRange:NSMakeRange(i, 1)]; //判读这个长度的字符串中是否包含数字以及标点符号 if([temp isEqualToString:@"0"] || [temp isEqualToString:@"1"] || [temp isEqualToString:@"2"] || [temp isEqualToString:@"3"] || [temp isEqualToString:@"4"] || [temp isEqualToString:@"5"] || [temp isEqualToString:@"6"] || [temp isEqualToString:@"7"] || [temp isEqualToString:@"8"] || [temp isEqualToString:@"9"]|| [temp isEqualToString:@"."] || [temp isEqualToString:@"-"]){ //给符合条件的属性字符串添加颜色,字体 [attributedString setAttributes:[NSDictionary dictionaryWithObjectsAndKeys: color, NSForegroundColorAttributeName, [UIFont fontWithName:FONT_LANTING_JIANHEI size:size], NSFontAttributeName, nil] range:NSMakeRange(i, 1)]; } } //重新给Label的text传递处理好的属性字符串 label.attributedText = attributedString; }
时间: 2024-10-02 20:50:49