自己封裝的工具類一、NSString類型//手機(jī)號(hào)碼驗(yàn)證+(BOOL)phoneText:(NSString*)string{? ? /**? ? * 移動(dòng)號(hào)段正則表達(dá)式? ? */? ? NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";? ? /**? ? * 聯(lián)通號(hào)段正則表達(dá)式? ? */? ? NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";? ? /**? ? * 電信號(hào)段正則表達(dá)式? ? */? ? NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";? ? NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];? ? BOOL isMatch1 = [pred1 evaluateWithObject:string];? ? NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];? ? BOOL isMatch2 = [pred2 evaluateWithObject:string];? ? NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];? ? BOOL isMatch3 = [pred3 evaluateWithObject:string];? ? ? ? if (isMatch1 || isMatch2 || isMatch3) {? ? ? ? ? ? ? return YES;? ? }else{? ? ? ? ? ? ? ? return NO;? ? }}//漢字轉(zhuǎn)拼音+ (NSString *)transform:(NSString *)chinese{? ? NSMutableString *pinyin = [chinese mutableCopy];? ? CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);? ? CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO); //去掉空格? ? NSString * contentping = [[pinyin lowercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];? ? ? ? return contentping;}//時(shí)間計(jì)算 剛剛-幾分鐘前-幾小時(shí)前+(NSString *)compareCurrentTime:(NSString*)str{? ? if (ios8orLater) {? ? ? ? ? ? }else{? ? ? ? ? ? ? ? return @"版本過低";? ? }? ? //創(chuàng)建時(shí)間對(duì)象? ? NSDateFormatter * formatter = [[NSDateFormatter alloc] init];? ? formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";? ? //系統(tǒng)時(shí)間的字符串? ? NSDate * dateXT? = [NSDate date];? ? NSCalendar * calendarXT = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];? ? NSDateComponents *compXT = [calendarXT components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday fromDate:dateXT];? ? NSString * stringXT = [NSString stringWithFormat:@"%ld-%ld-%ld %ld:%ld:%ld",(long)compXT.year, (long)compXT.month, (long)compXT.day, (long)compXT.hour, (long)compXT.minute, (long)compXT.second];? ? //系統(tǒng)時(shí)間? ? NSDate *dateTimeXT = [formatter dateFromString:stringXT];? ? //發(fā)帖日期? ? NSDate *date0 = [formatter dateFromString:str];? ? //初始化日歷? ? NSCalendar * calendar;? ? if (ios8orLater) {? ? ? ? calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];? ? }else{? ? ? ? ? ? ? ? calendar = [NSCalendar autoupdatingCurrentCalendar];? ? }? ? ? //拿到時(shí)間差(系統(tǒng)時(shí)間減去發(fā)帖時(shí)間)? ? NSDateComponents *com = [calendar components:NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond? fromDate:date0 toDate:dateTimeXT options:0];? ? //轉(zhuǎn)化成秒? ? NSTimeInterval timeInterval =(long)com.day *86400 + (long)com.hour *3600+(long)com.minute * 60+(long)com.second;? ? ? ? ? long temp = 0;? ? NSString *result;? ? //小于60 秒顯示剛剛? ? if (timeInterval < 60) {? ? ? ? ? ? ? ? result = [NSString stringWithFormat:@"剛剛"];? ? }? ? //小于60分鐘直接顯示幾分鐘前? ? else if((temp = timeInterval/60) <60){? ? ? ? ? ? ? ? result = [NSString stringWithFormat:@"%ld分鐘前",temp];? ? }? ? //小于24小時(shí)直接顯示幾小時(shí)前? ? else if((temp = timeInterval/(60*60)) <24){? ? ? ? ? ? ? ? result = [NSString stringWithFormat:@"%ld小時(shí)前",temp];? ? }? //小于7天顯示幾天前? ? else if((temp = timeInterval/(86400)) <7){? ? ? ? ? ? ? ? result = [NSString stringWithFormat:@"%ld天前",temp];? ? }? ? //大于7天小于14天顯示1周前? ? else if((temp = timeInterval/(86400*7)) <2){? ? ? ? ? ? ? ? result = [NSString stringWithFormat:@"%ld周前",temp];? ? }? ? else{? ? ? ? //直接顯示發(fā)帖日期? ? ? ? NSArray * arrayNew = [str? componentsSeparatedByString:@"-"];? ? ? ? //切日? ? ? ? NSArray * arrayday = [[arrayNew lastObject]? componentsSeparatedByString:@" "];? ? ? ? result =[NSString stringWithFormat:@"%@.%@.%@",[arrayNew firstObject],arrayNew[1],[arrayday firstObject]];? ? }? ? ? ? return result;}//根據(jù)日期判斷星座+(NSString *)getAstroWithMonth:(int)m day:(int)d{? ? ? ? NSString *astroString = @"魔羯水瓶雙魚白羊金牛雙子巨蟹獅子處女天秤天蝎射手魔羯";? ? NSString *astroFormat = @"102123444543";? ? NSString *result;? ? ? ? if (m<1||m>12||d<1||d>31){? ? ? ? return @"錯(cuò)誤日期格式!";? ? }? ? ? ? if(m==2 && d>29)? ? {? ? ? ? return @"錯(cuò)誤日期格式!!";? ? }else if(m==4 || m==6 || m==9 || m==11) {? ? ? ? ? ? ? ? if (d>30) {? ? ? ? ? ? return @"錯(cuò)誤日期格式!!!";? ? ? ? }? ? }? ? ? ? result=[NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(m*2-(d < [[astroFormat substringWithRange:NSMakeRange((m-1), 1)] intValue] - (-19))*2,2)]];? ? ? ? ? ? return result;}#pragma mark ============== 秒殺時(shí)間 =====================+(NSString*)timeCha:(NSNumber*)start Endtime:(NSNumber*)endtime{? ? NSString * string;? ? ? ? //開始時(shí)間? ? NSNumber * startDate? = start;? ? double sta = [startDate doubleValue];? ? ? ? //結(jié)束日期? ? NSNumber * endDate? = endtime;? ? double end = [endDate doubleValue];? ? ? ? //系統(tǒng)時(shí)間的毫秒? ? NSTimeInterval time=[[NSDate date] timeIntervalSince1970]*1000;? ? double now = time;? ? ? //NSTimeInterval返回的是double類型? ? //活動(dòng)未開始? ? if (now < sta) {? ? ? ? ? ? ? ? //得到還有多久開始的時(shí)間差時(shí)間差(秒)? ? ? ? int cha = (now - sta)/1000;? ? ? ? string = [NSString stringWithFormat:@"%d",cha];? ? ? ? ? ? }? ? ? ? //活動(dòng)進(jìn)行中? ? if (now > sta && now < end) {? ? ? ? //得到時(shí)間差(秒)? ? ? ? int cha = (end - now)/1000;? ? ? ? string = [NSString stringWithFormat:@"%d",cha];? ? }? ? ? ? //活動(dòng)已結(jié)束? ? if (now > end) {? ? ? ? ? ? ? ? string = @"已結(jié)束";? ? ? ? ? ? }? ? ? ? return string;? ? }#pragma mark ============== 已知開始時(shí)間和結(jié)束時(shí)間的毫秒,計(jì)算開始、進(jìn)行中、結(jié)束狀態(tài) =====================+(NSString*)timeStateStart:(double)sta Endtime:(double)end{? ? NSString * string;? ? //系統(tǒng)時(shí)間的毫秒? ? NSTimeInterval time=[[NSDate date] timeIntervalSince1970]*1000;? ? double now = time;? ? //NSTimeInterval返回的是double類型? ? //活動(dòng)未開始? ? if (now < sta) {? ? ? ? ? ? ? ? string = @"未開始";? ? ? ? ? ? }? ? ? ? //活動(dòng)進(jìn)行中? ? if (now > sta && now < end) {? ? ? ? ? ? ? string = @"進(jìn)行中";? ? }? ? ? ? //活動(dòng)已結(jié)束? ? if (now > end) {? ? ? ? ? ? ? ? string = @"已結(jié)束";? ? ? ? ? ? }? ? ? ? return string;}//通用正則匹配,可以匹配任意東西,只需要傳入正則表達(dá)式跟字符串即可+ (NSString *)processWithString:(NSString *)string regularExpression:(NSString *)regularExpression {? ? ? ? // 創(chuàng)建一個(gè)可變字符串,用來拼接符合條件的字符? ? NSMutableString *mString = [[NSMutableString alloc] init];? ? // 創(chuàng)建一個(gè)錯(cuò)誤對(duì)象? ? NSError *error;? ? // 正則? ? NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regularExpression options:0 error:&error];? ? // 如果正則沒有錯(cuò)誤進(jìn)行內(nèi)部的代碼? ? if (!error) {? ? ? ? // 將符合條件的字符位置記錄到數(shù)組中? ? ? ? NSArray *array = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];? ? ? ? // 利用數(shù)組中的位置將所有符合條件的字符拼接起來? ? ? ? for (NSTextCheckingResult *result in array) {? ? ? ? ? ? NSRange range = [result range];? ? ? ? ? ? NSString *mStr = [string substringWithRange:range];? ? ? ? ? ? [mString appendString:mStr];? ? ? ? }? ? ? ? return mString;? ? } else {? ? ? ? NSLog(@"error is %@", error);? ? }? ? return nil;}//傳入文本內(nèi)容、字體大小、控件寬度返回計(jì)算的文本占用位置的大?。ê瑢?,高)+(CGSize)sizeMyStrWith:(NSString*)text andFontSize:(CGFloat)fontsize andMineWidth:(CGFloat)Width{? ? //ios7? ? CGSize size = [text boundingRectWithSize:CGSizeMake(Width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontsize]} context:nil].size;? ? return size;? ? }//毫秒轉(zhuǎn)日期+(NSString * )dateChange:(NSNumber * )dateNum andFormat:(NSString*)formatStr{? ? //格式為 yyyy-MM-dd HH:mm:ss? ? //毫秒轉(zhuǎn)日期? ? NSString * stareStr = [NSString stringWithFormat:@"%@",dateNum];? ? double time= [stareStr doubleValue];? ? NSDate * d = [[NSDate alloc]initWithTimeIntervalSince1970:time/1000.0];? ? NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];? ? //日期樣式? ? dateFormatter.dateFormat = formatStr;? ? [dateFormatter stringFromDate:d];? ? //日期? ? NSString * newTime=[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:d]];? ? return newTime;}//日期轉(zhuǎn)毫秒+(NSString* )dateHMChange:(NSString*)dateString andFormat:(NSString*)format{? ? //日期轉(zhuǎn)毫秒(已經(jīng)沒有8個(gè)小時(shí)的時(shí)差了)//? ? 格式:yyyy-MM-dd HH:mm:ss? ? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];? ? formatter.dateFormat = format;? ? NSDate * Minedate = [formatter dateFromString:dateString];? ? NSTimeInterval timeStamp= [Minedate timeIntervalSince1970]*1000;? ? //NSTimeInterval返回的是double類型? ? double dateMs = timeStamp;//? ? NSLog(@"日期轉(zhuǎn)換的毫秒數(shù)為:%.f", dateMs);? ? return [NSString stringWithFormat:@"%.f",dateMs];}//字符串或者NSNumber判空方法(可傳入字符串類型和NSNumber類型)+(BOOL)isNULL:(id)stringOrNumber {? ? ? ? ? ? if ([stringOrNumber isKindOfClass:[NSNull class]]) {? ? ? ? ? ? return YES;? ? ? ? }? ? ? ? ? ? ? ? if (stringOrNumber == nil || stringOrNumber == NULL) {? ? ? ? ? ? return YES;? ? ? ? }? ? ? ? ? ? ? ? if ([stringOrNumber isEqual:[NSNull null]]) {? ? ? ? ? ? return YES;? ? ? ? }? ? ? ? ? ? ? ? if ([stringOrNumber isEqual:@"(null)"]) {? ? ? ? ? ? return YES;? ? ? ? }? ? ? ? ? ? ? ? if ([stringOrNumber isEqual:@""]) {
return YES;
}
if ([stringOrNumber isEqual:@""]) {
return YES;
}
if (!stringOrNumber) {
return YES;
}
//字符串才能判長度
if ([stringOrNumber isKindOfClass:[NSString class]]) {
NSString * stringGet = stringOrNumber;
if (stringGet.length==0) {
return YES;
}
//字符串專有判法
if ([[stringGet stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
}
return NO;
}
#pragma mark 同一個(gè)label中間幾個(gè)字 變顏色、大小都能改變
//UILabel中的內(nèi)容的部分字體顏色更改
//用法:self.integralLabel.attributedText =[NSString getLabelChangeColor:[UIColor orangeColor] andFont:[UIFont systemFontOfSize:22] andString1:@"我的" andChangeString:@"大的" andGetstring3:@"自己"];
+(NSMutableAttributedString*)getLabelChangeColor:(UIColor*)color andFont:(UIFont*)fout andString1:(NSString*)string1 andChangeString:(NSString*)string2 andGetstring3:(NSString*)string3{
//string2 是變色的部分
NSString *inteStr = [NSString stringWithFormat:@"%@%@%@",string1,string2,string3];
NSMutableAttributedString *inteMutStr = [[NSMutableAttributedString alloc] initWithString:inteStr];
//設(shè)置中間變紅的字體大小顏色
NSRange orangeRange = NSMakeRange([[inteMutStr string] rangeOfString:string2].location, [[inteMutStr string] rangeOfString:string2].length);
[inteMutStr addAttributes:@{NSFontAttributeName:fout,NSForegroundColorAttributeName:color} range:orangeRange];
return inteMutStr;
}