做iOS開發(fā)一年半了,自己整理了一些常用的分類。雖然已經(jīng)有前輩整理了更全的各種分類大全,但也正因為是大全所以導(dǎo)致過于龐大,會增加項目的體積。所以我自己就整理了一下項目中基本必用的分類,有效的提高開發(fā)效率,并且使得項目中的代碼更加簡潔。
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,分別設(shè)置圖標(biāo)按鈕、文字按鈕,按鈕位置,偏移量等屬性,并且集成了點擊回調(diào)
/**
圖標(biāo)UIBarButtonItem
@param type 左側(cè)or右側(cè)按鈕
@param norImage 按鈕圖標(biāo)
@param highImage 按鈕高亮圖標(biāo)
@param offset 圖標(biāo)偏移量
@param touchHandler 點擊事件處理
*/
+ (instancetype)wh_itemWithType:(WHItemType)type
norImage:(NSString *)norImage
highImage:(NSString *)highImage
offset:(CGFloat)offset
actionHandler:(WHTouchedBarButtonItemBlock)touchHandler;
/**
文字UIBarButtonItem
@param type 左側(cè)or右側(cè)按鈕
@param norTitle 普通狀態(tài)文字
@param font 文字字體大小
@param norColor 普通狀態(tài)文字顏色
@param highColor 高亮狀態(tài)文字顏色
@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
設(shè)置按鈕圖片的位置,倒計時按鈕等
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 倒計時過程中顯示的標(biāo)題
@param waitTittle 倒計時完成顯示的標(biāo)題
*/
- (void)wh_startTime:(NSInteger )timeout title:(NSString *)tittle waitTittle:(NSString *)waitTittle;
5.UIImage
/**
根據(jù)顏色生成純色圖片
@param color 顏色
*/
+ (UIImage *)wh_imgWithColor:(UIColor *)color;
/** 生成圓形圖片 */
- (UIImage *)wh_roundImg;
/**
生成帶純色邊框的圓形圖片
@param borderColor 邊框顏色
@param borderWidth 邊框?qū)挾? */
- (UIImage *)wh_roundImgWithBorderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth;
/**
生成帶圖片邊框的圖片
@param borderImg 邊框背景圖
@param borderWidth 邊框?qū)挾? */
- (UIImage *)wh_roundImgWithBorderImg:(NSString *)borderImg borderWidth:(CGFloat)borderWidth;
Foundation
1.NSArry
/** 串行遍歷數(shù)組中所有元素 */
- (void)wh_each:(void (^)(id obj))block;
/** 并發(fā)遍歷容器中所有元素(不要求順序時使用,提高遍歷速度) */
- (void)wh_apply:(void (^)(id obj))block;
/** 數(shù)組遍歷 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)的元素,返回重新生成的數(shù)組 */
- (NSArray *)wh_select:(BOOL (^)(id object))block;
/** 剔除所有不符合block條件(讓block返回YES)的元素,返回重新生成的數(shù)組 */
- (NSArray *)wh_reject:(BOOL (^)(id object))block;
/** 返回元素的block映射數(shù)組 */
- (NSArray *)wh_map:(id (^)(id obj))block;
2.NSDate
/**
根據(jù)格式獲取當(dāng)前時間
@param format 時間格式(如:yyyy-MM-dd)
*/
+ (NSString *)wh_getCurrentTimeWithFormat:(NSString *)format;
/**
時間戳和格式轉(zhuǎn)成日期字符串
@param timestamp 時間戳
@param format 時間格式(如:yyyy-MM-dd)
*/
+ (NSString *)wh_getDateStirngWithTimestamp:(NSString *)timestamp Format:(NSString *)format;
/**
NSDate轉(zhuǎn)字符串
@param format 時間格式(如:yyyy-MM-dd)
*/
- (NSString *)wh_getDateStringWithFormat:(NSString *)format;
/**
返回day天后的日期(若day為負數(shù),則為|day|天前的日期)
@param day 指定的天數(shù)
*/
- (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 計算完成的回調(diào)
*/
+ (void)wh_getFileSize:(NSString *)dirPath completion:(WHCompletionBlock)completion;
/**
清理指定文件夾
@param dirPath 文件夾名
*/
+ (void)wh_clearDirPath:(NSString *)dirPath;
4.NSSString
常用的正則驗證
/**
* 驗證純數(shù)字
*/
- (BOOL)wh_isOnlyNumber;
/**
* 驗證純中文
*/
- (BOOL)wh_isOnlyChinese;
/**
* 驗證密碼:數(shù)字+英文(大小寫)
*/
- (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]
/** 生成顏色值 可以設(shè)置透明度 */
#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;
/** 由角度轉(zhuǎn)換弧度 */
#define wh_degreesToRadian(x) (M_PI * (x) / 180.0)
/** 由弧度轉(zhuǎn)換角度 */
#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,以后也會不斷的更新。要是有什么的寫不對,或者更優(yōu)的寫法請務(wù)必流言告訴我哦,謝謝大家!