@interface Helper : NSObject
//字符串文字的长度 +(CGFloat)widthOfString:(NSString *)string font:(UIFont*)font height:(CGFloat)height; //字符串文字的高度 +(CGFloat)heightOfString:(NSString *)string font:(UIFont*)font width:(CGFloat)width; //获取今天的日期:年月日 +(NSDictionary *)getTodayDate; //自定义 正则匹配 + (BOOL) justWithInitRegularly:(NSString *)Regularly Str:(NSString *)str; //邮箱 + (BOOL) justEmail:(NSString *)email; //手机号码验证 + (BOOL) justMobile:(NSString *)mobile; //车牌号验证 + (BOOL) justCarNo:(NSString *)carNo; //车型 + (BOOL) justCarType:(NSString *)CarType; //用户名 + (BOOL) justUserName:(NSString *)name; //密码 + (BOOL) justPassword:(NSString *)passWord; //昵称 + (BOOL) justNickname:(NSString *)nickname; //身份证号 + (BOOL) justIdentityCard: (NSString *)identityCard; //汉字 + (BOOL) justChineseCharacter:(NSString *)Chinese; //网址Url + (BOOL) justURlSite:(NSString *)urlSite; //IP + (BOOL) justIP:(NSString *)ip; //匹配流量ID + (BOOL) justFromID:(NSString *)fid; @end
#import "Helper.h" @implementation Helper +(CGFloat)widthOfString:(NSString *)string font:(UIFont *)font height:(CGFloat)height { NSDictionary * dict=[NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName]; CGRect rect=[string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil]; return rect.size.width; } +(CGFloat)heightOfString:(NSString *)string font:(UIFont *)font width:(CGFloat)width { CGRect bounds; NSDictionary * parameterDict=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; bounds=[string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:parameterDict context:nil]; return bounds.size.height; } #pragma mark - 获取当天的日期:年月日 + (NSDictionary *)getTodayDate { //获取今天的日期 NSDate *today = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSCalendarUnit unit = kCFCalendarUnitYear|kCFCalendarUnitMonth|kCFCalendarUnitDay; NSDateComponents *components = [calendar components:unit fromDate:today]; NSString *year = [NSString stringWithFormat:@"%ld", (long)[components year]]; NSString *month = [NSString stringWithFormat:@"ld", (long)[components month]]; NSString *day = [NSString stringWithFormat:@"ld", (long)[components day]]; NSMutableDictionary *todayDic = [[NSMutableDictionary alloc] init]; [todayDic setObject:year forKey:@"year"]; [todayDic setObject:month forKey:@"month"]; [todayDic setObject:day forKey:@"day"]; return todayDic; } //邮箱 + (BOOL) justEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+//.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; return [emailTest evaluateWithObject:email]; } //手机号码验证 + (BOOL) justMobile:(NSString *)mobile { //手机号以13,14, 15,18 ,17开头,八个 /d 数字字符 NSString *phoneRegex = @"^((13[0-9])|(14[0-9])|(15[^4,//D])|(18[0,0-9])|(17[0,0-9]))//d{8}$"; NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex]; return [phoneTest evaluateWithObject:mobile]; } //车牌号验证 + (BOOL) justCarNo:(NSString *)carNo { NSString *carRegex = @"^[/u4e00-/u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_/u4e00-/u9fa5]$"; NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex]; // NSLog(@"carTest is %@",carTest); return [carTest evaluateWithObject:carNo]; } //车型 + (BOOL) justCarType:(NSString *)CarType { NSString *CarTypeRegex = @"^[/u4E00-/u9FFF]+$"; NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex]; return [carTest evaluateWithObject:CarType]; } //用户名 + (BOOL) justUserName:(NSString *)name { NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$"; NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex]; BOOL B = [userNamePredicate evaluateWithObject:name]; return B; } //密码 + (BOOL) justPassword:(NSString *)passWord { NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$"; NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex]; return [passWordPredicate evaluateWithObject:passWord]; } //昵称 + (BOOL) justNickname:(NSString *)nickname { NSString *nicknameRegex = @"^[/u4e00-/u9fa5]{4,8}$"; NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex]; return [passWordPredicate evaluateWithObject:nickname]; } //身份证号 + (BOOL) justIdentityCard: (NSString *)identityCard { BOOL flag; if (identityCard.length 0) { return YES; } } return NO; }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/98551.html