在工作中整理的一些分類與工具類亮蒋,分享給大家扣典。這些工具類可以減少項(xiàng)目中的代碼量,讓代碼變得更簡(jiǎn)潔慎玖,可以大大的提升項(xiàng)目的效率贮尖,直接拖到項(xiàng)目中使用即可。下載地址:https://github.com/leeSmile/tools/tree/master/textTools 歡迎start
分類
NSString+LXExtension
1.根據(jù)文件名計(jì)算文件大小
2.快速生成緩存/文檔/臨時(shí)目錄路徑
3.根據(jù)文字返回文本占用的高度/寬度
4.MD5加密
/**
* md5加密
*/
+ (NSString*)md5HexDigest:(NSString*)input;
/**
* 根據(jù)文件名計(jì)算出文件大小
*/
- (unsigned long long)lx_fileSize;
/**
* 生成緩存目錄全路徑
*/
- (instancetype)cacheDir;
/**
* 生成文檔目錄全路徑
*/
- (instancetype)docDir;
/**
* 生成臨時(shí)目錄全路徑
*/
- (instancetype)tmpDir;
/**
* @brief 根據(jù)字?jǐn)?shù)的不同,返回UILabel中的text文字需要占用多少Size
* @param size 約束的尺寸
* @param font 文本字體
* @return 文本的實(shí)際尺寸
*/
- (CGSize)textSizeWithContentSize:(CGSize)size font:(UIFont *)font;
/**
* @brief 根據(jù)文本字?jǐn)?shù)/文本寬度約束/文本字體 求得text的Size
* @param width 寬度約束
* @param font 文本字體
* @return 文本的實(shí)際高度
*/
- (CGFloat)textHeightWithContentWidth:(CGFloat)width font:(UIFont *)font;
/**
* @brief 根據(jù)文本字?jǐn)?shù)/文本寬度約束/文本字體 求得text的Size
* @param height 寬度約束
* @param font 文本字體
* @return 文本的實(shí)際長(zhǎng)度
*/
- (CGFloat)textWidthWithContentHeight:(CGFloat)height font:(UIFont *)font;
UIImage+LXExtension
1.快速生成圓形圖片
2.給定一個(gè)不要渲染的圖片名稱趁怔,生成一個(gè)最原始的圖片
3.模糊效果
4.固定寬高
5.剪切圖片某一部分
6.將自身填充到指定的size
- (instancetype)lx_circleImage;
// 生成一個(gè)圓形圖片
+ (instancetype)lx_circleImageNamed:(NSString *)name;
// 給定一個(gè)不要渲染的圖片名稱,生成一個(gè)最原始的圖片
+ (UIImage *)imageWithOriginalImageName:(NSString *)imageName;
// 模糊效果(渲染很耗時(shí)間,建議在子線程中渲染)
- (UIImage *)blurImage;
- (UIImage *)blurImageWithMask:(UIImage *)maskImage;
- (UIImage *)blurImageWithRadius:(CGFloat)radius;
- (UIImage *)blurImageAtFrame:(CGRect)frame;
// 灰度效果
- (UIImage *)grayScale;
// 固定寬度與固定高度
- (UIImage *)scaleWithFixedWidth:(CGFloat)width;
- (UIImage *)scaleWithFixedHeight:(CGFloat)height;
// 平均的顏色
- (UIColor *)averageColor;
// 裁剪圖片的一部分
- (UIImage *)croppedImageAtFrame:(CGRect)frame;
// 將自身填充到指定的size
- (UIImage *)fillClipSize:(CGSize)size;
UIView+LXExtension
1.快速設(shè)置控件frame
2.快速根據(jù)xib生成View
3.判斷兩個(gè)view是否重疊
/**
* 快速設(shè)置控件的位置
*/
@property (nonatomic, assign) CGSize lx_size;
@property (nonatomic, assign) CGFloat lx_width;
@property (nonatomic, assign) CGFloat lx_height;
@property (nonatomic, assign) CGFloat lx_x;
@property (nonatomic, assign) CGFloat lx_y;
@property (nonatomic, assign) CGFloat lx_centerX;
@property (nonatomic, assign) CGFloat lx_centerY;
/**
* 快速根據(jù)xib創(chuàng)建View
*/
+ (instancetype)lx_viewFromXib;
/**
* 判斷self和view是否重疊
*/
- (BOOL)lx_intersectsWithView:(UIView *)view;
UITextField+LXExtension
1.設(shè)置textField的占位文字顏色
/** 占位文字顏色 */
@property (nonatomic, strong) UIColor *lx_placeholderColor;
UIBarButtonItem+LXExtension
1.快速自定義導(dǎo)航欄上的按鈕
+ (instancetype)lx_itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action;
NSDate+LXExtension
1.兩個(gè)時(shí)間之間的時(shí)間間隔
2.是否為今天湿硝,昨天,明天
3.當(dāng)前是周幾
@interface LXDateItem : NSObject
@property (nonatomic, assign) NSInteger day;
@property (nonatomic, assign) NSInteger hour;
@property (nonatomic, assign) NSInteger minute;
@property (nonatomic, assign) NSInteger second;
@end
@interface NSDate (LXExtension)
- (LXDateItem *)lx_timeIntervalSinceDate:(NSDate *)anotherDate;
- (BOOL)lx_isToday;
- (BOOL)lx_isYesterday;
- (BOOL)lx_isTomorrow;
- (BOOL)lx_isThisYear;
//獲取今天周幾
- (NSInteger)getNowWeekday;
@end
NSDictionary+PropertyCode
1.根據(jù)字典快速生成Property屬性
使用場(chǎng)景:根據(jù)網(wǎng)絡(luò)請(qǐng)求返回的字典數(shù)據(jù)润努,寫對(duì)應(yīng)的模型关斜。當(dāng)屬性多時(shí),用手寫很費(fèi)功夫铺浇,可用這個(gè)類快速打印出所有的模型屬性痢畜,直接粘貼即可
// 生成所需要的屬性代碼
- (void)propertyCode;
NSObject+JSON
1.字典或?qū)ο筠D(zhuǎn)成JSON字符串?dāng)?shù)據(jù)
/**
* 字典或?qū)ο筠D(zhuǎn)成JSON字符串?dāng)?shù)據(jù)
*/
@property (nonatomic, copy, readonly) NSString *JSONString;
工具類
Single:快速創(chuàng)建一個(gè)單例
#ifndef Single_h
#define Single_h
#ifdef __OBJC__
#pragma mark - 單例模式 .h文件內(nèi)容
#define SingleInterface(name) +(instancetype)share##name;
#pragma mark - 單例模式 .m文件內(nèi)容
#if __has_feature(objc_arc)
#define SingleImplementation(name) +(instancetype)share##name {return [[self alloc]init];} \\
+ (instancetype)allocWithZone:(struct _NSZone *)zone { \\
static id instance; \\
static dispatch_once_t onceToken; \\
dispatch_once(&onceToken, ^{ \\
instance = [super allocWithZone:zone]; \\
}); \\
return instance; \\
} \\
- (id)copyWithZone:(NSZone *)zone{return self;} \\
- (id)mutableCopyWithZone:(NSZone *)zone {return self;}
#else
#define SingleImplementation(name) +(instancetype)share##name {return [[self alloc]init];} \\
+ (instancetype)allocWithZone:(struct _NSZone *)zone { \\
static id instance; \\
static dispatch_once_t onceToken; \\
dispatch_once(&onceToken, ^{ \\
instance = [super allocWithZone:zone]; \\
}); \\
return instance; \\
} \\
- (id)copyWithZone:(NSZone *)zone{return self;} \\
- (id)mutableCopyWithZone:(NSZone *)zone {return self;} \\
- (instancetype)retain {return self;} \\
- (instancetype)autorelease {return self;} \\
- (oneway void)release {} \\
- (NSUInteger)retainCount {return MAXFLOAT;} \\
#endif
#endif
#endif /* Single_h */
HelperUtil:常用的正則表達(dá)式
#pragma 正則匹配郵箱號(hào)
+ (BOOL)checkMailInput:(NSString *)mail;
#pragma 正則匹配手機(jī)號(hào)
+ (BOOL)checkTelNumber:(NSString *) telNumber;
#pragma 正則匹配用戶密碼6-18位數(shù)字和字母組合
+ (BOOL)checkPassword:(NSString *) password;
#pragma 正則匹配用戶姓名,20位的中文或英文
+ (BOOL)checkUserName : (NSString *) userName;
#pragma 正則匹配用戶身份證號(hào)
+ (BOOL)checkUserIdCard: (NSString *) idCard;
#pragma 正則匹員工號(hào),12位的數(shù)字
+ (BOOL)checkEmployeeNumber : (NSString *) number;
#pragma 正則匹配URL
+ (BOOL)checkURL : (NSString *) url;
#pragma 正則匹配昵稱
+ (BOOL) checkNickname:(NSString *) nickname;
#pragma 正則匹配以C開頭的18位字符
+ (BOOL) checkCtooNumberTo18:(NSString *) nickNumber;
#pragma 正則匹配以C開頭字符
+ (BOOL) checkCtooNumber:(NSString *) nickNumber;
#pragma 正則匹配銀行卡號(hào)是否正確
+ (BOOL) checkBankNumber:(NSString *) bankNumber;
#pragma 正則匹配17位車架號(hào)
+ (BOOL) checkCheJiaNumber:(NSString *) CheJiaNumber;
#pragma 正則只能輸入數(shù)字和字母
+ (BOOL) checkTeshuZifuNumber:(NSString *) CheJiaNumber;
#pragma 車牌號(hào)驗(yàn)證
+ (BOOL) checkCarNumber:(NSString *) CarNumber;
LXFileManagerp:將對(duì)象取歸檔到沙盒,讀取存儲(chǔ)刪除沙盒及偏好設(shè)置的數(shù)據(jù)
/// 文件管理類
@interface LXFileManager : NSObject
/// 把對(duì)象歸檔存到沙盒里
+(void)saveObject:(id)object byFileName:(NSString*)fileName;
/// 通過文件名從沙盒中找到歸檔的對(duì)象
+(id)getObjectByFileName:(NSString*)fileName;
/// 根據(jù)文件名刪除沙盒中的 plist 文件
+(void)removeFileByFileName:(NSString*)fileName;
/// 存儲(chǔ)用戶偏好設(shè)置 到 NSUserDefults
+(void)saveUserData:(id)data forKey:(NSString*)key;
/// 讀取用戶偏好設(shè)置
+(id)readUserDataForKey:(NSString*)key;
/// 刪除用戶偏好設(shè)置
+(void)removeUserDataForkey:(NSString*)key;
LXMacro:便利宏,便捷定義@property屬性
/** copy */
//NSString
#define String_(name) \\
zzn_copy_property(NSString*,name)
//NSArray
#define Array_(name) \\
zzn_copy_property(NSArray*,name)
//NSDictionary
#define Dictionary_(name) \\
zzn_copy_property(NSDictionary*,name)
//NSNumber,它沒用對(duì)應(yīng)的不可變類丁稀,其實(shí)用copy或strong沒有區(qū)別
#define Number_(name) \\
zzn_copy_property(NSNumber*,name)
//NSData
#define Data_(name) \\
zzn_copy_property(NSData*,name)
//NSSet
#define Set_(name) \\
zzn_copy_property(NSSet*,name)
//NSIndexSet
#define IndexSet_(name) \\
zzn_copy_property(NSIndexSet*,name)
//代碼塊吼拥,名稱和傳參,沒有傳參就不填
#define Block_(name,...) \\
zzn_set_block(void,name,__VA_ARGS__)
//有返回值的代碼塊
#define BlockReturn_(name,returnClass,...) \\
zzn_set_block(returnClass,name,__VA_ARGS__)
………………………………
LocalPushCenter:快速使用本地通知
+ (NSDate *)fireDateWithWeek:(NSInteger)week
hour:(NSInteger)hour
minute:(NSInteger)minute
second:(NSInteger)second;
//本地發(fā)送推送(先取消上一個(gè) 再push現(xiàn)在的)
+ (void)localPushForDate:(NSDate *)fireDate
forKey:(NSString *)key
alertBody:(NSString *)alertBody
alertAction:(NSString *)alertAction
soundName:(NSString *)soundName
launchImage:(NSString *)launchImage
userInfo:(NSDictionary *)userInfo
badgeCount:(NSUInteger)badgeCount
repeatInterval:(NSCalendarUnit)repeatInterval;
#pragma mark - 退出
+ (void)cancelAllLocalPhsh;
+ (void)cancleLocalPushWithKey:(NSString *)key;```