美文网首页
iOS 常用代码集合(一)

iOS 常用代码集合(一)

作者: FORMAT_qi | 来源:发表于2018-03-07 16:15 被阅读5次
    退回输入键盘
    [textField resignFirstResponder];
    
    隐藏状态栏
    [[UIApplication shareApplication] setStatusBarHidden: YES animated:NO]
    
    横屏
    [[UIApplication shareApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight].
     
    屏幕变动检测
    orientation == UIInterfaceOrientationLandscapeLeft
    
    自动适应父视图大小:
    aView.autoresizingSubviews = YES;
    aView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                              UIViewAutoresizingFlexibleHeight);
    
    判断邮箱格式是否正确的代码
    利用正则表达式验证
    -(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;
        
    状态栏的网络活动风火轮是否旋转
       [UIApplication sharedApplication].networkActivityIndicatorVisible,默认值是NO。
    
    多线程和结束后的更新UI操作
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        耗时操作
     
        dispatch_async(dispatch_get_main_queue(), ^{
            更新UI操作
     
        });
        
    });
     
    修改PlaceHolder的默认颜色
    [username_text setValue:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5] forKeyPath:@"_placeholderLabel.textColor"];
    
    /**
     *  正则验证
     **/
    +(BOOL) string:(NSString *)source MatchRegex:(NSString *) exp
    {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", exp];
        return [predicate evaluateWithObject:source];
    }
     
     
    /**
     *  获取正则表达式中匹配的个数
     **/
    + (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];
    }
     
     
    /**
     *  从文件路径中分解出文件的扩展名
     **/
    + (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];
    }
     
    判断是否为浮点形
    + (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;
    }
    

    相关文章

      网友评论

          本文标题:iOS 常用代码集合(一)

          本文链接:https://www.haomeiwen.com/subject/chwjfftx.html