/**
正则判断手机号码地址格式
@param mobileNum 传入的手机号
@return 是否格式正确
*/
+ (BOOL)isMobileNumber:(NSString *)mobileNum {
//电信号段:133/149/153/173/174/177/180/181/189/199
//联通号段:130/131/132/145/146/155/156/166/171/175/176/185/186
//移动号段:134/135/136/137/138/139/147/148/150/151/152/157/158/159/178//182/183/184/187/188/198
//虚拟运营商:170
NSString *MOBILE = @"^1(3[0-9]|4[5-9]|5[0-35-9]|6[6]|7[0-13-8]|8[0-9]|9[89])\\d{8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
return [regextestmobile evaluateWithObject:mobileNum];
}
/**
正则判断手机号码所属运营商
@param phoneNum 传入的手机号
@return 运营商名称
*/
+ (NSString *)judgePhoneNumber:(NSString *)phoneNum {
NSString *CM = @"^1(3[4-9]|4[78]|5[0-17-9]|7[8]|8[2-47-8]|9[8])\\d{8}$";
NSString *CU = @"^1(3[0-2]|4[56]|5[56]|6[6]|7[156]|8[56])\\d{8}$";
NSString *CT = @"^1(3[3]|4[9]|5[3]|7[347]|8[019]|9[9])\\d{8}$";
NSString *XN = @"^1(7[0])\\d{8}$";
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
NSPredicate *regextestxn = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", XN];
if ([regextestcm evaluateWithObject:phoneNum] == YES){
//移动
return @"中国移动";
} else if ([regextestct evaluateWithObject:phoneNum] == YES){
//电信
return @"中国电信";
} else if ([regextestcu evaluateWithObject:phoneNum] == YES){
//联通
return @"中国联通";
} else if ([regextestxn evaluateWithObject:phoneNum] == YES) {
//虚拟号码段
return @"虚拟号码段";
} else{
//可能还有未收录全的三大运营商号码,无法识别,后期会添加
return @"暂时未知";
}
}
/**
正则判断密码格式
@param text 传入的密码
@return 是否符合格式
*/
+ (BOOL)judgePassWordLegal:(NSString *)text{
BOOL result = false;
if ([text length] >= 6){
NSString *regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
result = [pred evaluateWithObject:text];
}
return result;
压缩图片到指定大小
@param image 需要压缩的图片
@param maxLength 目标图片质量大小
@return 压缩后得到的图片
*/
+ (UIImage *)compressImageQuality:(UIImage *)image
toByte:(NSInteger)maxLength {
CGFloat compression = 1;
NSData *data = UIImageJPEGRepresentation(image, compression);
if (data.length < maxLength) return image;
CGFloat max = 1;
CGFloat min = 0;
for (int i = 0; i < 6; ++i) {
compression = (max + min) / 2;
data = UIImageJPEGRepresentation(image, compression);
if (data.length < maxLength * 0.9) {
min = compression;
} else if (data.length > maxLength) {
max = compression;
} else {
break;
}
}
UIImage *resultImage = [UIImage imageWithData:data];
return resultImage;
}
/**
图片裁剪
@param input 需要处理的图片
@param width 需要达到的宽度
@return 处理后的图片
*/
+ (UIImage *)image:(UIImage *)input
scaleToWidth:(CGFloat)width {
// 如果传入的宽度比当前宽度还要大,就直接返回
if (width > input.size.width) {
return input;
}
// 计算缩放之后的高度
CGFloat height = (width / input.size.width) * input.size.height;
// 初始化要画的大小
CGRect rect = CGRectMake(0, 0, width, height);
// 1. 开启图形上下文
// UIGraphicsBeginImageContext(rect.size);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
// 2. 画到上下文中 (会把当前image里面的所有内容都画到上下文)
[input drawInRect:rect];
// 3. 取到图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 4. 关闭上下文
UIGraphicsEndImageContext();
// 5. 返回
return image;
}
/**
替换字符串中指定位置字符
@param string 需要替换的字符串
@param character 替换成的字符
@param from 起始位置
@param len 需要替换的长度
@return 替换以后的字符串
*/
+ (NSString *)replaceString:(NSString *)string
withChar:(NSString *)character
fromIndex:(NSInteger)from
length:(NSInteger )len {
if (string.length == 0) {
string = @"";
} else
string = [string stringByReplacingCharactersInRange:NSMakeRange(from, len) withString:character];
return string;
正则判断手机号码地址格式
@param mobileNum 传入的手机号
@return 是否格式正确
*/
+ (BOOL)isMobileNumber:(NSString *)mobileNum {
//电信号段:133/149/153/173/174/177/180/181/189/199
//联通号段:130/131/132/145/146/155/156/166/171/175/176/185/186
//移动号段:134/135/136/137/138/139/147/148/150/151/152/157/158/159/178//182/183/184/187/188/198
//虚拟运营商:170
NSString *MOBILE = @"^1(3[0-9]|4[5-9]|5[0-35-9]|6[6]|7[0-13-8]|8[0-9]|9[89])\\d{8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
return [regextestmobile evaluateWithObject:mobileNum];
}
/**
正则判断手机号码所属运营商
@param phoneNum 传入的手机号
@return 运营商名称
*/
+ (NSString *)judgePhoneNumber:(NSString *)phoneNum {
NSString *CM = @"^1(3[4-9]|4[78]|5[0-17-9]|7[8]|8[2-47-8]|9[8])\\d{8}$";
NSString *CU = @"^1(3[0-2]|4[56]|5[56]|6[6]|7[156]|8[56])\\d{8}$";
NSString *CT = @"^1(3[3]|4[9]|5[3]|7[347]|8[019]|9[9])\\d{8}$";
NSString *XN = @"^1(7[0])\\d{8}$";
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
NSPredicate *regextestxn = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", XN];
if ([regextestcm evaluateWithObject:phoneNum] == YES){
//移动
return @"中国移动";
} else if ([regextestct evaluateWithObject:phoneNum] == YES){
//电信
return @"中国电信";
} else if ([regextestcu evaluateWithObject:phoneNum] == YES){
//联通
return @"中国联通";
} else if ([regextestxn evaluateWithObject:phoneNum] == YES) {
//虚拟号码段
return @"虚拟号码段";
} else{
//可能还有未收录全的三大运营商号码,无法识别,后期会添加
return @"暂时未知";
}
}
/**
正则判断密码格式
@param text 传入的密码
@return 是否符合格式
*/
+ (BOOL)judgePassWordLegal:(NSString *)text{
BOOL result = false;
if ([text length] >= 6){
NSString *regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
result = [pred evaluateWithObject:text];
}
return result;
}
/**
压缩图片到指定大小
@param image 需要压缩的图片
@param maxLength 目标图片质量大小
@return 压缩后得到的图片
*/
+ (UIImage *)compressImageQuality:(UIImage *)image
toByte:(NSInteger)maxLength {
CGFloat compression = 1;
NSData *data = UIImageJPEGRepresentation(image, compression);
if (data.length < maxLength) return image;
CGFloat max = 1;
CGFloat min = 0;
for (int i = 0; i < 6; ++i) {
compression = (max + min) / 2;
data = UIImageJPEGRepresentation(image, compression);
if (data.length < maxLength * 0.9) {
min = compression;
} else if (data.length > maxLength) {
max = compression;
} else {
break;
}
}
UIImage *resultImage = [UIImage imageWithData:data];
return resultImage;
}
/**
图片裁剪
@param input 需要处理的图片
@param width 需要达到的宽度
@return 处理后的图片
*/
+ (UIImage *)image:(UIImage *)input
scaleToWidth:(CGFloat)width {
// 如果传入的宽度比当前宽度还要大,就直接返回
if (width > input.size.width) {
return input;
}
// 计算缩放之后的高度
CGFloat height = (width / input.size.width) * input.size.height;
// 初始化要画的大小
CGRect rect = CGRectMake(0, 0, width, height);
// 1. 开启图形上下文
// UIGraphicsBeginImageContext(rect.size);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
// 2. 画到上下文中 (会把当前image里面的所有内容都画到上下文)
[input drawInRect:rect];
// 3. 取到图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 4. 关闭上下文
UIGraphicsEndImageContext();
// 5. 返回
return image;
}
/**
替换字符串中指定位置字符
@param string 需要替换的字符串
@param character 替换成的字符
@param from 起始位置
@param len 需要替换的长度
@return 替换以后的字符串
*/
+ (NSString *)replaceString:(NSString *)string
withChar:(NSString *)character
fromIndex:(NSInteger)from
length:(NSInteger )len {
if (string.length == 0) {
string = @"";
} else
string = [string stringByReplacingCharactersInRange:NSMakeRange(from, len) withString:character];
return string;
}