iOS程序猿iOS学习笔记iOS开发

开发小知识点整理第三期(持续更新)

2018-12-09  本文已影响9人  coder小鹏

1.实时监测UITextField输入框输入内容的变化

//添加监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextFieldTextDidChangeNotification object:_inputTextField];
//监听回调
- (void)textFieldChanged:(NSNotification *)sender {
    
    UITextField *textField = sender.object;
    //进行相应的UI上的变化
}
//移除监听
-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

2.UILabel宽度和高度自适应的方法

//自适应宽度
- (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
    label.text = title;
    label.font = font;
    [label sizeToFit];
    return label.frame.size.width;
}
//自适应高度
+ (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont *)font
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.text = title;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return height;
}

3.获取视频第一帧

+ (UIImage*)getVideoPreViewImage:(NSURL *)path
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:path options:nil];
    
    AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    assetGen.appliesPreferredTrackTransform = YES;
    
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *videoImage = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return videoImage;
    
}

4.UILabel,UIButton,NSURL,UITextField赋值时出现空值时防止崩溃的处理方式,分别给对应的类添加分类

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        Method originalM = class_getInstanceMethod([self class], @selector(setText:));
        Method swzM = class_getInstanceMethod([self class], @selector(sw_setText:));
        method_exchangeImplementations(originalM, swzM);
    });
}

- (void)sw_setText:(NSString *)text
{
    
    if (![text isKindOfClass:[NSNull class]] && ![text isEqualToString:@"<null>"] && text != nil)
    {
        [self sw_setText:text];
    }
}
+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    
        Method originalM = class_getInstanceMethod([self class], @selector(setTitle:forState:));
        Method swzM = class_getInstanceMethod([self class], @selector(swz_setTitle:forState:));
        method_exchangeImplementations(originalM, swzM);
    });
}

- (void)swz_setTitle:(NSString *)title forState:(UIButtonType)state
{
    if (![title isKindOfClass:[NSNull class]] && ![title isEqualToString:@"<null>"] && title != nil)
    {
        [self swz_setTitle:title forState:state];
    }
}
+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method originalM = class_getInstanceMethod([self class], @selector(setText:));
        Method swzM = class_getInstanceMethod([self class], @selector(sw_setText:));
        method_exchangeImplementations(originalM, swzM);
    });
}

- (void)sw_setText:(NSString *)text
{
    if (![text isKindOfClass:[NSNull class]] && ![text isEqualToString:@"<null>"] && text != nil)
    {
        [self sw_setText:text];
    }
}
+ (void)load
{
    Method method1 = class_getClassMethod([NSURL class], @selector(sw_URLWithString:));
    Method method2 = class_getClassMethod([NSURL class], @selector(URLWithString:));
    method_exchangeImplementations(method1, method2);
}

+ (instancetype)sw_URLWithString:(NSString *)URLString
{
    if (![URLString isKindOfClass:[NSNull class]] && ![URLString isEqualToString:@"<null>"] && URLString != nil)
    {
        return [self sw_URLWithString:URLString];
    }
    else
    {
        return nil;
    }
}

5.字符串防空判断

BOOL isEmptyString;
if ([text == nil || [text isKindOfClass:[NSNull class]] || [text isEqualToString:@""]])
 {
    isEmptyString = YES;
 }

6.判断手机号的正确性

- (BOOL)isPhoneNumber
{
    NSString *MOBILE = @"^1(3[0-9]|4[4-9]|5[0-35-9]|7[0135-8]|8[0-9])\\d{8}$";
    NSPredicate *regextestMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [regextestMobile evaluateWithObject:self];
}

7.判断versionB相对与versionA是高版本还是低版本

/*
 * 判断versionB相对与versionA是高版本还是低版本
 * 返回: 
        NSOrderedDescending 低版本
        NSOrderedAscending 高版本
        NSOrderedSame 同一版本
 */
+ (NSComparisonResult)compareVersion:(NSString *)versionA withVersion:(NSString *)versionB
{
    if (nil == versionA && nil != versionB)
    {
        return NSOrderedAscending;
    }
    else if (nil != versionA && nil == versionB)
    {
        return NSOrderedDescending;
    }
    else if (nil == versionA && nil == versionB)
    {
        return NSOrderedSame;
    }
    
    NSComparisonResult result = NSOrderedSame;
    
    NSString* versionBackupA = versionA;
    NSString* versionBackupB = versionB;
    
    NSRange rangeA = [versionBackupA rangeOfString:@"."];
    NSRange rangeB = [versionBackupB rangeOfString:@"."];
    
    while (NSNotFound != rangeA.location && NSNotFound != rangeB.location)
    {
        NSString* absoluteVerA = [versionBackupA substringToIndex:(rangeA.location)];
        NSString* absoluteVerB = [versionBackupB substringToIndex:(rangeB.location)];
        
        if (nil == versionA && nil != versionB)
        {
            result = NSOrderedAscending;
            break;
        }
        else if (nil != versionA && nil == versionB)
        {
            result = NSOrderedDescending;
            break;
        }
        else if (nil == versionA && nil == versionB)
        {
            result = NSOrderedSame;
            break;
        }
        
        int intVerA = [absoluteVerA intValue];
        int intVerB = [absoluteVerB intValue];
        
        if (intVerA > intVerB)
        {
            result = NSOrderedDescending;
            break;
        }
        else if (intVerA < intVerB)
        {
            result = NSOrderedAscending;
            break;
        }
        else
        {
            versionBackupA = [versionBackupA substringFromIndex:(rangeA.location + 1)];
            versionBackupB = [versionBackupB substringFromIndex:(rangeB.location + 1)];
            
            rangeA = [versionBackupA rangeOfString:@"."];
            rangeB = [versionBackupB rangeOfString:@"."];
        }
    }
    
    return result;
}

8.判断设备是否支持摄像头

+ (BOOL)isOrientationPortrait
{
    return UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
}
上一篇下一篇

猜你喜欢

热点阅读