美文网首页
iOS开发随笔(一)---记录开发中的小方法

iOS开发随笔(一)---记录开发中的小方法

作者: 涂了摩丝丶 | 来源:发表于2018-08-19 13:01 被阅读12次
    1.判断字符串为空和只为空格解决办法
    + (BOOL)isBlankString:(NSString *)string
    {
        if  (string == nil)  {
    
            return YES;
    
        }
        
        if (string == NULL) {
            
            return YES;
            
        }
        
        if ([string isKindOfClass:[NSNull class]]) {
            
            return YES;
            
        }
        
        if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
            
            return YES;
            
        }
        if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@"null"]||[[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@"(null)"]) {
            
            return YES;
            
        }
        return NO;
        
    }
    
    2.限制textfiled 文字字数(textview同理)
    + (void)limitTextField:(UITextField *)textField limitNum:(NSUInteger)num
    {
        /**
         *  最大输入长度,中英文字符都按一个字符计算
         */
        UITextField *textfield = textField;
        NSString *toBeString = textfield.text;
        // 获取键盘输入模式
    
        NSString *lang = textfield.textInputMode.primaryLanguage;
    
        // 中文输入的时候,可能有markedText(高亮选择的文字),需要判断这种状态
    
        // zh-Hans表示简体中文输入, 包括简体拼音,健体五笔,简体手写
    
        if ([lang isEqualToString:@"zh-Hans"]) {
    
            UITextRange *selectedRange = [textfield markedTextRange];
            //获取高亮选择部分
            UITextPosition *position = [textfield positionFromPosition:selectedRange.start offset:0];
            // 没有高亮选择的字,表明输入结束,则对已输入的文字进行字数统计和限制
            if (!position) {
                if (toBeString.length > num) {
                    // 截取子串
                    textfield.text = [toBeString substringToIndex:num];
                }
            }
        } else {
            // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
            if (toBeString.length > num) {
                // 截取子串
                textfield.text = [toBeString substringToIndex:num];
            }
    
        }
    }
    
    3.判断时间 1过期 0相等 -1没过期
    +(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(@"oneDay : %@, anotherDay : %@", oneDay, anotherDay);
        if (result == NSOrderedDescending) {
            //在指定时间前面 过了指定时间 过期
            NSLog(@"oneDay  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;
    
    }
    
    5.相机获取到的图片自动旋转90度解决办法
    + (UIImage *)fixOrientation:(UIImage *)aImage {
    
        if (aImage.imageOrientation == UIImageOrientationUp)
            
            return aImage;
        
        CGAffineTransform transform = CGAffineTransformIdentity;
        
        switch (aImage.imageOrientation) {
                
            case UIImageOrientationDown:
                
            case UIImageOrientationDownMirrored:
                
                transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
                
                transform = CGAffineTransformRotate(transform, M_PI);
                
                break;
                
            
            case UIImageOrientationLeft:
                
            case UIImageOrientationLeftMirrored:
                
                transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
                
                transform = CGAffineTransformRotate(transform, M_PI_2);
                
                break;
                
                
                
            case UIImageOrientationRight:
                
            case UIImageOrientationRightMirrored:
                
                transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
                
                transform = CGAffineTransformRotate(transform, -M_PI_2);
                
                break;
                
            default:
                
                break;
                
        }
    
        switch (aImage.imageOrientation) {
                
            case UIImageOrientationUpMirrored:
                
            case UIImageOrientationDownMirrored:
                
                transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
                
                transform = CGAffineTransformScale(transform, -1, 1);
                
                break;
            
            case UIImageOrientationLeftMirrored:
                
            case UIImageOrientationRightMirrored:
                
                transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
                
                transform = CGAffineTransformScale(transform, -1, 1);
                
                break;
                
            default:
                
                break;
                
        }
        
        CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
                                                 
                                                 CGImageGetBitsPerComponent(aImage.CGImage), 0,
                                                 
                                                 CGImageGetColorSpace(aImage.CGImage),
                                                 
                                                 CGImageGetBitmapInfo(aImage.CGImage));
        
        CGContextConcatCTM(ctx, transform);
        
        switch (aImage.imageOrientation) {
                
            case UIImageOrientationLeft:
                
            case UIImageOrientationLeftMirrored:
                
            case UIImageOrientationRight:
                
            case UIImageOrientationRightMirrored:
                
                // Grr...
                
                CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
                
                break;
            
            default:
                
                CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
                
                break;
                
        }
        // And now we just create a new UIImage from the drawing context
        
        CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
        
        UIImage *img = [UIImage imageWithCGImage:cgimg];
        
        CGContextRelease(ctx);
        
        CGImageRelease(cgimg);
        
        return img;
        
    }
    

    相关文章

      网友评论

          本文标题:iOS开发随笔(一)---记录开发中的小方法

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