/**
*改变字符串中具体某字符串的颜色
*/
+ (void)messageAction:(UILabel*)theLabchangeString:(NSString*)changeandAllColor:(UIColor*)allColorandMarkColor:(UIColor*)markColorandMarkFondSize:(float)fontSize {
NSString*tempStr = theLab.text;
NSMutableAttributedString*strAtt = [[NSMutableAttributedStringalloc]initWithString:tempStr];
[strAttaddAttribute:NSForegroundColorAttributeNamevalue:allColorrange:NSMakeRange(0, [strAttlength])];
NSRange markRange = [tempStrrangeOfString:change];
[strAttaddAttribute:NSForegroundColorAttributeNamevalue:markColorrange:markRange];
[strAttaddAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"HelveticaNeue"size:fontSize]range:markRange];
theLab.attributedText= strAtt;
}
/*
*x*y
*改变字符start 和 end 之间的字符的颜色 和 字体大小
*/
+ (void)messageAction:(UILabel*)theLabstartString:(NSString*)startendString:(NSString*)endandAllColor:(UIColor*)allColorandMarkColor:(UIColor*)markColorandMarkFondSize:(float)fontSize {
NSString*tempStr = theLab.text;
NSMutableAttributedString*strAtt = [[NSMutableAttributedStringalloc]initWithString:tempStr];
[strAttaddAttribute:NSForegroundColorAttributeNamevalue:allColorrange:NSMakeRange(0, [strAttlength])];
// 'x''y'字符的范围
NSRange tempRange = NSMakeRange(0,0);
if([selfjudgeStringIsNull:start]) {
tempRange = [tempStrrangeOfString:start];
}
NSRange tempRangeOne = NSMakeRange([strAttlength],0);
if([selfjudgeStringIsNull:end]) {
tempRangeOne = [tempStrrangeOfString:end];
}
// 更改字符颜色
NSRange markRange = NSMakeRange(tempRange.location+tempRange.length, tempRangeOne.location-(tempRange.location+tempRange.length));
[strAttaddAttribute:NSForegroundColorAttributeNamevalue:markColorrange:markRange];
// 更改字体
// [strAtt addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20] range:NSMakeRange(0, [strAtt length])];
[strAttaddAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"HelveticaNeue"size:fontSize]range:markRange];
theLab.attributedText= strAtt;
}
/**
*改变字符串中所有数字的颜色
*/
+ (void)setRichNumberWithLabel:(UILabel*)labelColor:(UIColor*)colorFontSize:(CGFloat)size {
NSMutableAttributedString*attributedString = [[NSMutableAttributedStringalloc]initWithString:label.text];
NSString*temp =nil;
for(inti =0; i < [attributedStringlength]; i++) {
temp = [label.textsubstringWithRange:NSMakeRange(i,1)];
if([selfisPureInt:temp]) {
[attributedStringsetAttributes:[NSDictionarydictionaryWithObjectsAndKeys:
color, NSForegroundColorAttributeName,
[UIFontsystemFontOfSize:size],NSFontAttributeName,nilnil]
range:NSMakeRange(i,1)];
}
}
label.attributedText= attributedString;
}
/**
*此方法是用来判断一个字符串是不是整型.
*如果传进的字符串是一个字符,可以用来判断它是不是数字
*/
+ (BOOL)isPureInt:(NSString*)string {
NSScanner*scan = [NSScannerscannerWithString:string];
intvalue;
return[scanscanInt:&value] && [scanisAtEnd];
}
/**
*判断字符串是否不全为空
*/
+ (BOOL)judgeStringIsNull:(NSString*)string {
if([[stringclass]isSubclassOfClass:[NSNumberclass]]) {
returnYES;
}
BOOLresult =NO;
if(string !=nil&& string.length>0) {
for(inti =0; i < string.length; i ++) {
NSString*subStr = [stringsubstringWithRange:NSMakeRange(i,1)];
if(![subStrisEqualToString:@" "] && ![subStrisEqualToString:@""]) {
result =YES;
}
}
}
returnresult;
}