iOS 項目中最常用的一些工具類

來源:互聯網
上載者:User

標籤:

#pragma mark  密碼sha1加密

+(NSString * )sha1WithInputStr:(NSString * )inputStr{

    const char * cstr = [inputStr cStringUsingEncoding:NSUTF8StringEncoding];

    NSData * data = [NSData dataWithBytes:cstr length:inputStr.length];

    

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    CC_SHA1(data.bytes, (unsigned int)data.length, digest);

    NSMutableString * outputStr = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

    for (int i = 0 ; i< CC_SHA1_DIGEST_LENGTH; i++) {

        [outputStr appendFormat:@"%02x",digest[i]];

    }

    return outputStr;

}

 

#pragma mark  正則判斷手機號是否正確

+ (BOOL)validateNumber:(NSString *) textString

{

    NSString* [email protected]"^1[3|4|5|7|8][0-9]\\d{8}$";

    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];

    return [numberPre evaluateWithObject:textString];

}

 

#pragma mark 正則判斷社會安全號碼

+ (BOOL) validateIdentityCard: (NSString *)identityCard

{

    BOOL flag;

    if (identityCard.length <= 0) {

        flag = NO;

        return flag;

    }

    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";

    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];

    return [identityCardPredicate evaluateWithObject:identityCard];

}

 

 

 

#pragma mark 正則判斷快遞單號

+ (BOOL) validateEMSnumber: (NSString *)EMSnumber

{

    BOOL flag;

    if (EMSnumber.length <= 0) {

        flag = NO;

        return flag;

    }

    NSString * reg = @"^[0-9a-zA-Z]{10,}$";

    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",reg];

    return [identityCardPredicate evaluateWithObject:EMSnumber];

}

 

 

 

#pragma mark 正則判斷取現金額

+ (BOOL) validateCashMoney: (NSString *)cashMoney

{

    BOOL flag;

    if (cashMoney.length <= 0) {

        flag = NO;

        return flag;

    }

    NSString * reg = @"^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){1,2})?$";

    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",reg];

    return [identityCardPredicate evaluateWithObject:cashMoney];

}

 

#pragma mark - 正則判斷100的整數倍

+ (BOOL)judge100IntegerTimes:(NSString *)str

{

    NSString *remainderStr = [NSString stringWithFormat:@"%d", [str intValue] % 100];

    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^[0]$"];

    return [numberPre evaluateWithObject:remainderStr];

    

}

 

#pragma mark 正則判斷登入密碼是否正確(6-20位元字字母結合)

+ (BOOL)validateNumberOrLetter:(NSString *) textString

{

    BOOL result = false;

    if ([textString length] >= 6){

        

        NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$";

        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

        result = [pred evaluateWithObject:textString];

    }

    return result;

}

 

 

#pragma mark 正則簡單判斷銀行卡號是否正確(16-19位元字)

+ (BOOL)validateBankCardNumber:(NSString *)textString

{

    NSString* [email protected]"^([0-9]{16}|[0-9]{19})$";

    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];

    return [numberPre evaluateWithObject:textString];

}

 

 

#pragma mark 手機號碼中間變*

+(NSString * )cipherchangeText:(NSString * )changetext firstOne:(NSInteger )firstOne lenght:(NSInteger)lenght{

    NSString * number = [changetext stringByReplacingCharactersInRange:NSMakeRange(firstOne, lenght) withString:@"****"];

    return number;

}

 

 

#pragma mark 擷取手機UUID

+(NSString * )uuid{

    NSString * uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

    return uuid;

}

 

 

#pragma mark 自適應高度

+(CGRect)sizeOfText:(NSString * )text Width:(CGFloat )width Font:(CGFloat)fontFloat{

    NSDictionary * textDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontFloat],NSFontAttributeName, nil];

    CGRect rect  = [text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading  attributes:textDic context:nil];

    return rect;

}

 

 

#pragma mark  自適應寬度

+(CGRect)sizeOfText:(NSString * )text Height:(CGFloat)height Font:(CGFloat)fontFloat{

    NSDictionary * textDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontFloat],NSFontAttributeName, nil];

    CGRect rect = [text boundingRectWithSize:CGSizeMake(0, height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:textDic context:nil];

    return rect;

}

 

#pragma mark  圖片等比壓縮

+ (CGFloat)zoomImageScaleWithImage:(UIImage * )image withWidth:(CGFloat )width

{

    UIImage *newImage;

    

    //判斷如果圖片的SIZE的寬度大於螢幕的寬度就讓它壓縮

//    if (image.size.width > width) {

        //開始壓縮圖片

        CGSize size = image.size;

        

        UIGraphicsBeginImageContext(CGSizeMake(width, width * size.height / size.width));

        

        [image drawInRect:CGRectMake(0, 0, width, width * size.height / size.width)];

        

        newImage = UIGraphicsGetImageFromCurrentImageContext();

        

        UIGraphicsEndImageContext();

        

 

//    }

        return newImage.size.height;

}

 

#pragma mark 建立回收鍵盤表徵圖

+ (void)createReciveKeyBoardToolBarWithView:(id)view{//回收鍵盤

    UIView * aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Width_FullScreen, 30)];

    aView.backgroundColor = nil;

    

    BlockButton * btn = [BlockButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(Width_FullScreen - 35 , 2, 30, 25);

    [btn setImage:[UIImage imageNamed:@"closekeyboard"] forState:UIControlStateNormal];

    [btn setBlock:^(BlockButton * btn){

        

        [view endEditing:YES];

                

    }];

    

    [aView addSubview:btn];

    [view setInputAccessoryView:aView];

    

}

 

iOS 項目中最常用的一些工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.