快捷操作

去除数组中重复的对象

NSArray*newArr=[oldArr valueForKeyPath:@“@distinctUnionOfObjects.self"];

获取window

+(UIWindow*)getWindow{

    UIWindow*win=nil;

    //[UIApplication sharedApplication].keyWindow;

    for(id item in [UIApplication sharedApplication].windows){

        if([itemclass]==[UIWindowclass]){

            if(!((UIWindow*)item).hidden){

                win=item;

                break;

            }

        }

    }

    returnwin;

}

修改textField的placeholder的字体颜色、大小

[textField setValue:[UIColor redColor]forKeyPath:@"_placeholderLabel.textColor"];

[textField setValue:[UIFont boldSystemFontOfSize:16]forKeyPath:@"_placeholderLabel.font"];

清理app缓存

-(void)handleClearView{//删除两部分//1.删除 sd 图片缓存//先清除内存中的图片缓存

    [[SDImageCache sharedImageCache]clearMemory];

    //清除磁盘的缓存

    [[SDImageCache sharedImageCache]clearDisk];

    //2.删除自己缓存

    NSString*myCachePath=[NSHomeDirectory()stringByAppendingPathComponent:@"Library/Caches"];[[NSFileManager defaultManager]removeItemAtPath:myCachePath error:nil];

}

身份证号验证

-(BOOL)validateIdentityCard{

    BOOLflag;

//    if(self.length <=0){

//        flag=NO;

//        return flag;

//

//    }

    NSString*regex2=@"^(\\d{14}|\\d{17})(\\d|[xX])$";

    NSPredicate*identityCardPredicate=[NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex2];

    return[identityCardPredicateevaluateWithObject:self];

}

KVO监听某个对象的属性

    // 添加监听者

    [self addObserver:self forKeyPath:property options:NSKeyValueObservingOptionNew context:nil];

    // 当监听的属性值变化的时候会来到这个方法

-(void)observeValueForKeyPath:(NSString*)keyPathofObject:(id)objectchange:(NSDictionary*)changecontext:(void*)context{

    if([keyPathisEqualToString:@"property"]){

//        [self textViewTextChange];

    }else{}

}

移除字符串中的空格和换行

+(NSString*)removeSpaceAndNewline:(NSString*)str{

    NSString*temp=[str stringByReplacingOccurrencesOfString:@" "withString:@""];

    temp=[tempstringByReplacingOccurrencesOfString:@"\r"withString:@""];

    temp=[tempstringByReplacingOccurrencesOfString:@"\n"withString:@""];

    return temp;

}

UILabel设置内边距

-(void)drawTextInRect:(CGRect)rect{// 边距,上左下右

    UIEdgeInsetsinsets={0,5,0,5};

    [selfdrawTextInRect:UIEdgeInsetsInsetRect(rect,insets)];

}

为UIView某个角添加圆角// 左上角和右下角添加圆角

    UIBezierPath*maskPath = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerBottomRight )cornerRadii:CGSizeMake(20,20)];

    CAShapeLayer*maskLayer = [CAShapeLayer layer];

    maskLayer.frame=self.view.bounds;

    maskLayer.path= maskPath.CGPath;

    self.view.layer.mask= maskLayer;

UITextView中打开或禁用复制,剪切,选择,全选等功能

// 继承UITextView重写这个方法

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{// 返回NO为禁用,YES为开启

    // 粘贴

    if(action==@selector(paste:))returnNO;

    // 剪切

    if(action==@selector(cut:))returnNO;

    // 复制

    if(action==@selector(copy:))returnNO;

    // 选择

    if(action==@selector(select:))returnNO;

    // 选中全部

    if(action==@selector(selectAll:))returnNO;

    // 删除

    if(action==@selector(delete:))returnNO;

    // 分享

    if(action==@selector(share))returnNO;

    return [super canPerformAction:action withSender:sender];

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容