iOS开发常用工具类

关于正则表达式判断:

1.手机号校验

 NSString *regex = @"^((13[0-9])|(14[0-9])|(17[0-9])|(15[0-9])|(18[0-9]))\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:str];
    if (!isMatch) {请输入正确的手机号码}

2.传真、固话校验

 //验证输入的固话中不带 "-"符号
    NSString * strNum = @"^(0[0-9]{2,3})?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\\d{8}$)";  
    //验证输入的固话中带 "-"符号
    NSString * strNum1 = @"^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\\d{8}$)";
    NSPredicate *checktest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strNum];
    NSPredicate *checktest1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strNum1];
if(checktest evaluateWithObject:number]||[checktest1 evaluateWithObject:number){
  请输入正确的传真或固话号码
}

3.车牌号校验

NSString *regex = @"^[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [pred evaluateWithObject:str];    
    if (!isMatch) {请输入正确的车牌号}

4.身份证号码校验

 NSString *regex = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
 BOOL isMatch = [pred evaluateWithObject:str];
 if (!isMatch) {请输入正确的身份证号码}

5.ip地址校验

 NSString *regex = @"^(http|https)://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
 NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [pred evaluateWithObject:str];
if (!isMatch) {ip地址错误}

6.延迟执行的block

+ (void)performBlock:(void (^)(void))block afterDelay:(NSTimeInterval)delay
{
    block = [block copy];
    [self performSelector:@selector(fireBlockAfterDelay:) withObject:block afterDelay:delay];
}

7.图片缩放到指定大小尺寸

+ (UIImage *)imageFromView:(UIView *)viewBac
{
    UIGraphicsBeginImageContextWithOptions(viewBac.bounds.size, YES, viewBac.layer.contentsScale);
    [viewBac.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    return image;
}

8.根据文字获取label大小

+ (CGSize)getLabelSizeWithText:(NSString*)str AndSize:(CGSize)siz AndFont:(UIFont*)font {
    CGSize size;
    if ([[[UIDevice currentDevice] systemVersion] intValue]>=7.0) {
        NSDictionary *attribute = @{NSFontAttributeName: font};
        size = [str boundingRectWithSize:siz options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    }
    else{
        size = [str sizeWithFont:font constrainedToSize:siz lineBreakMode:NSLineBreakByCharWrapping];
    }
 
    return size;
}

8.时间戳转时间

+ (NSString*)timestampToString:(NSString*)stamp Format:(NSString*)format{
    
    if (stamp.length==13) {
        stamp = [stamp substringToIndex:10];
    }
    if (![stamp isKindOfClass:[NSNumber class]]) {
        if (stamp.length==0) {
            return @"";
        }
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    NSArray *weekdayAry = [NSArray arrayWithObjects:@"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    [dateFormatter setShortWeekdaySymbols:weekdayAry];
    
    if (format) {
        [dateFormatter setDateFormat:format];
    }
    
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:[stamp intValue]];
    
    NSString *dateStr = [dateFormatter stringFromDate:time];
    return dateStr;
}

9.获取当前系统的时间戳

+(long)getTimeSp{
    long time;
    NSDate *fromdate=[NSDate date];
    time=(long)[fromdate timeIntervalSince1970];
    return time;
}

10.比较日期大小

+(int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //NSLog(@"Date1  is in the future");
        return 1;
    }
    else if (result == NSOrderedAscending){
        //NSLog(@"Date1 is in the past");
        return -1;
    }
    //NSLog(@"Both dates are the same");
    return 0;
}

11.判断字符串是否为空

+ (BOOL) isBlankString:(NSString *)string {
    if (string == nil || string == NULL || [string isEqual:[NSNull null]]) {
        return YES;
    }
    if ([string isEqualToString:@""]) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

12.根据图片颜色生成图片

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

13.合成图片

+ (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
    CGImageRef sourceImageRef = [image CGImage];
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    return newImage;
}

------以上方法有借鉴别人之处,仅供互相学习。

推荐阅读更多精彩内容