iOS 常用代碼集合(一)

退回輸入鍵盤
[textField resignFirstResponder];

隱藏狀態(tài)欄
[[UIApplication shareApplication] setStatusBarHidden: YES animated:NO]

橫屏
[[UIApplication shareApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight].
 
屏幕變動(dòng)檢測(cè)
orientation == UIInterfaceOrientationLandscapeLeft

自動(dòng)適應(yīng)父視圖大小:
aView.autoresizingSubviews = YES;
aView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                          UIViewAutoresizingFlexibleHeight);

判斷郵箱格式是否正確的代碼
利用正則表達(dá)式驗(yàn)證
-(BOOL)isValidateEmail:(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];
}
 
壓縮圖片
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
    Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    Tell the old image to draw in this newcontext, with the desired
    new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    End the context
    UIGraphicsEndImageContext();
    Return the new image.
    return newImage;
}
鍵盤透明
   textField.keyboardAppearance = UIKeyboardAppearanceAlert;
    
狀態(tài)欄的網(wǎng)絡(luò)活動(dòng)風(fēng)火輪是否旋轉(zhuǎn)
   [UIApplication sharedApplication].networkActivityIndicatorVisible,默認(rèn)值是NO。

多線程和結(jié)束后的更新UI操作
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    耗時(shí)操作
 
    dispatch_async(dispatch_get_main_queue(), ^{
        更新UI操作
 
    });
    
});
 
修改PlaceHolder的默認(rèn)顏色
[username_text setValue:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5] forKeyPath:@"_placeholderLabel.textColor"];

/**
 *  正則驗(yàn)證
 **/
+(BOOL) string:(NSString *)source MatchRegex:(NSString *) exp
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", exp];
    return [predicate evaluateWithObject:source];
}
 
 
/**
 *  獲取正則表達(dá)式中匹配的個(gè)數(shù)
 **/
+ (NSInteger) getMatchCount:(NSString *)text inRegx:(NSString *)exp
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:exp options:0 error:nil];
    
    int count = 0;
    if (regex != nil) {
        NSArray *array = [regex matchesInString:text options:NSMatchingReportProgress range:NSMakeRange(0, [text length])];
        
        for(int i=0; i< [array count]; i++)
        {
            NSTextCheckingResult *tcr = [array objectAtIndex:i];
            NSRange range = [tcr range];
            count += range.length;
        }
    }
    return count;
}

/**
 *  從文件路徑中分解出文件名
 **/
+ (NSString *) splitFileNameForPath:(NSString *)filePath
{
    NSArray *array = [filePath componentsSeparatedByString:@"/"];
    return [array lastObject];
}
 
 
/**
 *  從文件路徑中分解出文件的擴(kuò)展名
 **/
+ (NSString *) getFileExtension:(NSString *)filePath
{
    NSString *fileName = [self splitFileNameForPath:filePath];
    NSArray *array = [fileName componentsSeparatedByString:@"."];
    return [NSString stringWithFormat:@".%@",[array lastObject]];
}

判斷是否為整形
+ (BOOL)isPureInt:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}
 
判斷是否為浮點(diǎn)形
+ (BOOL)isPureFloat:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    return [scan scanFloat:&val] && [scan isAtEnd];
}
 
/**
 *  版本比較
 **/
+ (BOOL)isVersion:(NSString*)versionA biggerThanVersion:(NSString*)versionB
{
    NSArray *arrayNow = [versionB componentsSeparatedByString:@"."];
    NSArray *arrayNew = [versionA componentsSeparatedByString:@"."];
    BOOL isBigger = NO;
    NSInteger i = arrayNew.count > arrayNow.count? arrayNow.count : arrayNew.count;
    NSInteger j = 0;
    BOOL hasResult = NO;
    for (j = 0; j < i; j ++) {
        NSString* strNew = [arrayNew objectAtIndex:j];
        NSString* strNow = [arrayNow objectAtIndex:j];
        if ([strNew integerValue] > [strNow integerValue]) {
            hasResult = YES;
            isBigger = YES;
            break;
        }
        if ([strNew integerValue] < [strNow integerValue]) {
            hasResult = YES;
            isBigger = NO;
            break;
        }
    }
    if (!hasResult) {
        if (arrayNew.count > arrayNow.count) {
            NSInteger nTmp = 0;
            NSInteger k = 0;
            for (k = arrayNow.count; k < arrayNew.count; k++) {
                nTmp += [[arrayNew objectAtIndex:k]integerValue];
            }
            if (nTmp > 0) {
                isBigger = YES;
            }
        }
    }
    return isBigger;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容