自写常用扩展类

转载请注明出处!!!

这里版本较老,请查看GitHub。里面会随时更新代码。

好久没有写博客了,今天闲着没事就把前一段时间写的几个扩展类发一下。主要写了三个扩展类。

一、UIColor+WCYAdd

这个类主要是为系统的UIColor添加一些自定方法。在这里我添加了一个生成随机颜色和根据十六进制生成颜色两个方法。

/**
 生成随机颜色,并返回

 @return 随机颜色
 */
+ (UIColor *)randomColor;


/**
 根据16进制取颜色
 支持@“#123456”、 @“0X123456”、 @“123456”三种格式

 @param color 16进制数
 @return 颜色
 */
+ (UIColor *)colorWithHexString:(NSString *)color;

调用方法很简单,就像系统方法一样。

    self.view.backgroundColor = [UIColor randomColor];

    self.view.backgroundColor = [UIColor colorWithHexString:@"0x123456"];

二、UIImage+WCYAdd

这个类主要添加了根据颜色生成图片和生成渐变颜色图片的方法。

/**
 根据颜色生成图片

 @param color 图片颜色
 @return 图片
 */
+ (UIImage *)getImageWithColor:(UIColor *)color;


/**
 颜色渐变
 
 @param rect 尺寸
 @param startColor 开始颜色
 @param endColor 结束颜色
 @return 渐变图片
 */
+ (UIImage *)colorgradientWithRect:(CGRect)rect startColor:(UIColor *)startColor endColor:(UIColor *)endColor;

调用方法同上,直接调用即可。

三、UIView+Touch

这个类是为UIview类添加点击事件做的。需要和NSObject+Addtion相配合。

// 前提需要打开交互
self.textLabel.userInteractionEnabled = YES; [self.textLabel setTouchAction:^(UIView *view) { NSLog(@"label 点击"); }];

四、NSDate+WCYTimaAdd

这个类是为NSDate类添加的一些方法。可以直接获取时分秒、字符串和时间戳的转化、判断两日期是否相同、两时间的前后和类似微信根据时间返回刚刚几分钟前这样的文字。

// 字符串和date的互相转化
// 使用这两个方法要注意时间格式
+ (NSDate *)stringToDate:(NSString *)string;
+ (NSString *)dateToString:(NSDate *)date;

// 返回时分秒
- (NSUInteger)hour;
- (NSUInteger)minite;
- (NSTimeInterval)second;

// 判断两日期是否相同
- (BOOL)isSameDateWithData:(NSDate *)date;

// 判断时间前后
+ (NSString *)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay;

/*
 < 60s 刚刚
 < 60Min n分钟前
 < 24h n小时前
 < 1W n天前
 > 1W yy-MM-dd hh:mm
 */
- (NSString *)easyReadPastTimeString;

使用方法:

NSDate *currentData = [NSDate date];
    NSLog(@"%f",currentData.second);
    
    NSString * str1 = @"2018-05-19 11:08:05";
    NSString * str2 = @"2018-03-28 08:44:05";

    NSLog(@"%@   
%@",[NSDate stringToDate:str1],[NSDate stringToDate:str2]);

    NSLog(@"%@",[NSDate compareOneDay:[NSDate stringToDate:str1] withAnotherDay:[NSDate stringToDate:str2]]);
    
    NSLog(@"%@",[[NSDate stringToDate:str1] easyReadPastTimeString]);
    
    NSLog(@"%d",[currentData isSameDateWithData:[NSDate dateWithTimeIntervalSinceNow:100000000]]);
    NSLog(@"%@",[NSDate dateToString:currentData]);

 附件已过期,请查看我GitHub

原文地址:https://www.cnblogs.com/weicyNo-1/p/9059719.html