iOS代碼片段庫

【1】鍵盤遮擋的一種解決方法:
主要實現邏輯: 將需要顯示的控件放置在一個單獨的UIView中,然後整個view上移處理
代碼實現:
+(void)animationView:(UIScrollView *)anmaView activeRect:(CGRect)filedRect
{
    //    CGRect filedRect=filed.frame;
    CGPoint point=anmaView.contentOffset;
    //    計算出距離屏幕上方的相對座標高度
    float  realDis=filedRect.origin.y-point.y;
    //    計算到目標點的距離的絕對值
    int distance1=0;
    if (iPhone5) {
        distance1=120;
    }else{
        distance1=90;
    }
    float distance=fabsf(realDis-distance1);
    if (realDis>distance1) {
        point.y+=distance;
    }else{
        point.y-=distance;
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    anmaView.contentOffset=point;
    [UIView commitAnimations];
}

【2】移動號碼驗證(網絡轉載,已驗證)
- (BOOL)isMobileNumber:(NSString*)mobileNum
{
    /**
     * 手機號碼
     * 移動:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 聯通:130,131,132,152,155,156,185,186
     * 電信:133,1349,153,180,189
     */
    NSString* MOBILE= @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     * 中國移動:China Mobile
     * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     */
    NSString* CM= @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     * 中國聯通:China Unicom
     * 130,131,132,152,155,156,185,186
     */
    NSString* CU= @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     * 中國電信:China Telecom
     * 133,1349,153,180,189
     */
    NSString* CT= @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     * 大陸地區固話及小靈通
     * 區號:010,020,021,022,023,024,025,027,028,029
     * 號碼:七位或八位
     */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
   
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CT];
   
    if(([regextestmobile evaluateWithObject:mobileNum] == YES)
       || ([regextestcm evaluateWithObject:mobileNum] == YES)
       || ([regextestct evaluateWithObject:mobileNum] == YES)
       || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}


【3】郵箱驗證(網絡轉載,未驗證)
-(BOOL)validateEmail:(NSString*)email  
{  
    if((0 != [email rangeOfString:@"@"].length) &&  
         (0 != [email rangeOfString:@"."].length))  
    {  
             
           NSCharacterSet* tmpInvalidCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];  
        NSMutableCharacterSet* tmpInvalidMutableCharSet = [[tmpInvalidCharSet mutableCopy] autorelease];  
        [tmpInvalidMutableCharSet removeCharactersInString:@"_-"];  
         
           //使用compare option 來設定比較規則,如  
           //NSCaseInsensitiveSearch是不區分大小寫  
           //NSLiteralSearch 進行完全比較,區分大小寫  
           //NSNumericSearch 只比較定符串的個數,而不比較字符串的字面值  
        NSRange range1 = [email rangeOfString:@"@"  
                                              options:NSCaseInsensitiveSearch];  
         
        //取得用戶名部分  
        NSString* userNameString = [email substringToIndex:range1.location];  
        NSArray* userNameArray   = [userNameString componentsSeparatedByString:@"."];  
             
        for(NSString* string in userNameArray)  
           {  
            NSRange rangeOfInavlidChars = [string rangeOfCharacterFromSet: tmpInvalidMutableCharSet];  
            if(rangeOfInavlidChars.length != 0 || [string isEqualToString:@""])  
                return NO;  
        }  
         
        NSString *domainString = [email substringFromIndex:range1.location+1];  
        NSArray *domainArray   = [domainString componentsSeparatedByString:@"."];  
         
        for(NSString *string in domainArray)  
           {  
            NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:tmpInvalidMutableCharSet];  
            if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])  
                return NO;  
        }  
       
        return YES;  
    }  
    else // no ''@'' or ''.'' present  
        return NO;  
}  
   
   
BOOL NSStringIsValidEmail(NSString *checkString)    
{    
    NString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";  
    NSString *laxString = @".+@.+\.[A-Za-z]{2}[A-Za-z]*";    
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];    
    return [emailTest evaluateWithObject:checkString];    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章