好文iOSOC进化

让iOS开发更高效

2017-01-04  本文已影响409人  苏丶小洛

做iOS开发一年半了,自己整理了一些常用的分类。虽然已经有前辈整理了更全的各种分类大全,但也正因为是大全所以导致过于庞大,会增加项目的体积。所以我自己就整理了一下项目中基本必用的分类,有效的提高开发效率,并且使得项目中的代码更加简洁。
cocoapods:

pod 'WHCategories'

github地址:https://github.com/WaferWang/WHCategories

UIKit

1.UIView

@property (nonatomic, assign) CGPoint wh_origin;
@property (nonatomic, assign) CGSize wh_size;

@property (nonatomic, assign) CGFloat wh_width;
@property (nonatomic, assign) CGFloat wh_height;

@property (nonatomic, assign) CGFloat wh_centerX;
@property (nonatomic, assign) CGFloat wh_centerY;

@property (nonatomic, assign) CGFloat wh_top;
@property (nonatomic, assign) CGFloat wh_left;
@property (nonatomic, assign) CGFloat wh_bottom;
@property (nonatomic, assign) CGFloat wh_right;
/** 添加单击手势 */
- (void)wh_addTapActionWithBlock:(WHGestureActionBlock)block;
/** 添加长安手势 */
- (void)wh_addLongPressActionWithBlock:(WHGestureActionBlock)block;

2.UITextField

/* 占位文字颜色 */
@property (nonatomic, strong) UIColor *wh_placeholderColor;
/* 占位文字字体 */
@property (nonatomic, strong) UIFont *wh_placeholderFont;

3.UIBarButtonItem

一行代码搞定UIBarButtonItem,分别设置图标按钮、文字按钮,按钮位置,偏移量等属性,并且集成了点击回调

/**
 图标UIBarButtonItem
 @param type         左侧or右侧按钮
 @param norImage     按钮图标
 @param highImage    按钮高亮图标
 @param offset       图标偏移量
 @param touchHandler 点击事件处理
 */
+ (instancetype)wh_itemWithType:(WHItemType)type
                       norImage:(NSString *)norImage
                      highImage:(NSString *)highImage
                         offset:(CGFloat)offset
                  actionHandler:(WHTouchedBarButtonItemBlock)touchHandler;

/**
 文字UIBarButtonItem
 @param type         左侧or右侧按钮
 @param norTitle     普通状态文字
 @param font         文字字体大小
 @param norColor     普通状态文字颜色
 @param highColor    高亮状态文字颜色
 @param offset       偏移量
 @param touchHandler 点击事件处理
 */
+ (instancetype)wh_itemWithType:(WHItemType)type
                       norTitle:(NSString *)norTitle
                           font:(CGFloat)font
                       norColor:(UIColor *)norColor
                      highColor:(UIColor *)highColor
                         offset:(CGFloat)offset
                  actionHandler:(WHTouchedBarButtonItemBlock)touchHandler;

4.UIButton

设置按钮图片的位置,倒计时按钮等

typedef NS_ENUM(NSInteger, WHImagePosition) {
    WHImagePositionLeft = 0,                //图片在左,文字在右(默认)
    WHImagePositionRight = 1,               //图片在右,文字在左
    WHImagePositionTop = 2,                 //图片在上,文字在下
    WHImagePositionBottom = 3,              //图片在下,文字在上
};

/**
 *  @param postion 图片的位置
 *  @param spacing 图片和文字的间隔
 */
- (void)wh_setImagePosition:(WHImagePosition)postion spacing:(CGFloat)spacing;
/**
 让按钮进入倒计时
 @param timeout 倒计时长
 @param tittle 倒计时过程中显示的标题
 @param waitTittle 倒计时完成显示的标题
 */
- (void)wh_startTime:(NSInteger )timeout title:(NSString *)tittle waitTittle:(NSString *)waitTittle;

5.UIImage

/**
 根据颜色生成纯色图片
 @param color 颜色
 */
+ (UIImage *)wh_imgWithColor:(UIColor *)color;

/** 生成圆形图片 */
- (UIImage *)wh_roundImg;

/**
 生成带纯色边框的圆形图片
 @param borderColor 边框颜色
 @param borderWidth 边框宽度
 */
- (UIImage *)wh_roundImgWithBorderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth;

/**
 生成带图片边框的图片
 @param borderImg 边框背景图
 @param borderWidth 边框宽度
 */
- (UIImage *)wh_roundImgWithBorderImg:(NSString *)borderImg borderWidth:(CGFloat)borderWidth;

Foundation

1.NSArry

/** 串行遍历数组中所有元素 */
- (void)wh_each:(void (^)(id obj))block;

/** 并发遍历容器中所有元素(不要求顺序时使用,提高遍历速度) */
- (void)wh_apply:(void (^)(id obj))block;

/** 数组遍历 obj:元素 idx:索引 */
- (void)wh_eachWithIndex:(void (^)(id obj, NSUInteger idx))block;

/** 返回第一个符合block条件(让block返回YES)的元素 */
- (nullable id)wh_match:(BOOL (^)(id obj))block;

/** 筛选所有符合block条件(让block返回YES)的元素,返回重新生成的数组 */
- (NSArray *)wh_select:(BOOL (^)(id object))block;

/** 剔除所有不符合block条件(让block返回YES)的元素,返回重新生成的数组 */
- (NSArray *)wh_reject:(BOOL (^)(id object))block;

/** 返回元素的block映射数组 */
- (NSArray *)wh_map:(id (^)(id obj))block;

2.NSDate

/**
 根据格式获取当前时间
 @param format 时间格式(如:yyyy-MM-dd)
 */
+ (NSString *)wh_getCurrentTimeWithFormat:(NSString *)format;

/**
 时间戳和格式转成日期字符串
 @param timestamp 时间戳
 @param format    时间格式(如:yyyy-MM-dd)
 */
+ (NSString *)wh_getDateStirngWithTimestamp:(NSString *)timestamp Format:(NSString *)format;

/**
 NSDate转字符串
 @param format 时间格式(如:yyyy-MM-dd)
 */
- (NSString *)wh_getDateStringWithFormat:(NSString *)format;

/**
 返回day天后的日期(若day为负数,则为|day|天前的日期)
 @param day 指定的天数
 */
- (NSDate *)wh_dateAfterDay:(NSUInteger)day;

/**
 返回指定日期的date
 @param year 指定的年
 @param month 指定的月
 @param day 指定的天
 */
+ (NSDate *)wh_dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day;

3.NSFileManager

/** 获取document路径 */
+ (NSString *)wh_documentsPath;

/** 获取library路径 */
+ (NSString *)wh_libraryPath;

/** 获取caches路径 */
+ (NSString *)wh_cachesPath;

/** 计算缓存文件夹大小 */
+ (void)wh_getCacheSizeCompletion:(WHCompletionBlock)completion;

/** 清除缓存文件夹 */
+ (void)wh_clearCache;

/**
 计算指定文件夹的大小
 
 @param dirPath 文件夹名
 @param completion 计算完成的回调
 */
+ (void)wh_getFileSize:(NSString *)dirPath completion:(WHCompletionBlock)completion;

/**
 清理指定文件夹
 
 @param dirPath 文件夹名
 */
+ (void)wh_clearDirPath:(NSString *)dirPath;

4.NSSString

常用的正则验证

/**
 *  验证纯数字
 */
- (BOOL)wh_isOnlyNumber;
/**
 *  验证纯中文
 */
- (BOOL)wh_isOnlyChinese;
/**
 *  验证密码:数字+英文(大小写)
 */
- (BOOL)wh_isPwd;
/**
 *  手机号有效性
 */
- (BOOL)wh_isMobileNumber;

等等...

常用宏

/** 屏幕宽度 */
#define wh_screenWidth [UIScreen mainScreen].bounds.size.width

/** 屏幕高度 */
#define wh_screenHeight [UIScreen mainScreen].bounds.size.height

/** 生成颜色值 */
#define wh_RGB(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]

/** 生成颜色值 可以设置透明度 */
#define wh_RGBA(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]

/** 弱引用 */
#define wh_weakSelf(type) __weak typeof(type) weak##type = type;

/** 强引用 */
#define wh_strongSelf(type)  __strong typeof(type) type = weak##type;

/** 由角度转换弧度 */
#define wh_degreesToRadian(x) (M_PI * (x) / 180.0)

/** 由弧度转换角度 */
#define wh_radianToDegrees(radian) (radian * 180.0) / (M_PI)

/** 定义UIImage对象 */
#define wh_mageNamed(imageName) [UIImage imageNamed:imageName]

/** 打印输出 */
#if DEBUG
#define wh_Log(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define wh_Log(FORMAT, ...) nil
#endif

等等...

详情请参考github:https://github.com/WaferWang/WHCategories

文章里先介绍这么多,具体的请移步github,以后也会不断的更新。要是有什么的写不对,或者更优的写法请务必流言告诉我哦,谢谢大家!

上一篇下一篇

猜你喜欢

热点阅读