?這段時間開發(fā)任務(wù)比較少(也許產(chǎn)品快死寥假,或許我們很牛X)有些許時間來分享一些開發(fā)中的事情和大家一起學(xué)習(xí)進步。之前我也是一名拿來主義,現(xiàn)在我也要懂得回饋了茉继,閑話少敘,還是直奔主題蚀乔。
?今天跟大家分享的是提高開發(fā)效率常用到的工具類(直接拿去用烁竭,不用謝):
//計算當(dāng)前月份的第一天
+(NSString *)getFirstDayOfThisMonth
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *satrtDate;
[calendar rangeOfUnit:NSCalendarUnitMonth
startDate:&satrtDate
interval:nil
forDate:[NSDate date]];
NSDateFormatter *firstDayFormatter = [[NSDateFormatter alloc] init];
[firstDayFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *firstDay = [firstDayFormatter stringFromDate:satrtDate];
return firstDay;
}
// 正則判斷,判斷輸入的必須是中文
+ (BOOL)matchStringFormat:(NSString *)matchedStr withRegex:(NSString *)regex
{
//SELF MATCHES一定是大寫
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [predicate evaluateWithObject:matchedStr];
}
//數(shù)組轉(zhuǎn)json格式
+ (NSString *)arrayToJSONString:(NSArray *)array
{
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array
options:NSJSONWritingPrettyPrinted
error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
return jsonString;
}
//字典轉(zhuǎn)json格式
+ (NSString *)dictionaryToJSONString:(NSDictionary *)dictionary
{
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
options:NSJSONWritingPrettyPrinted
error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
return jsonString;
}
/**
計算兩個時間差
@param localTime 獲取本地時間
@param endTime 結(jié)束時間
@return 時間差
*/
+ (NSDictionary *)customTimeWithLocalTime:(NSDate *)localTime
endTime:(NSDate *)endTime
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
NSString *dateTime = [formatter stringFromDate:localTime];
NSLog(@"當(dāng)前時間%@============年-月-日 時:分:秒=====================",dateTime);
// NSDate *endTime = [NSDate dateWithTimeInterval:timeInterval
// sinceDate:localTime];
NSString *endTimeStr = [formatter stringFromDate:endTime];
NSLog(@"最終時間%@============年-月-日 時:分:秒=====================",endTimeStr);
//計算時間間隔(單位是秒)
NSTimeInterval time = [endTime timeIntervalSinceDate:localTime];
//計算天數(shù)、時吉挣、分派撕、秒
NSInteger days = ((NSInteger)time)/(3600*24);
NSInteger hours = ((NSInteger)time)%(3600*24)/3600;
NSInteger minutes = ((NSInteger)time)%(3600*24)%3600/60;
NSInteger seconds = ((NSInteger)time)%(3600*24)%3600%60;
NSNumber *day = [NSNumber numberWithInteger:days];
NSNumber *hour = [NSNumber numberWithInteger:hours];
NSNumber *minute = [NSNumber numberWithInteger:minutes];
NSNumber *second = [NSNumber numberWithInteger:seconds];
NSString *dateContent = [[NSString alloc] initWithFormat:@"相差時間%li天%li小時%li分%li秒",(long)days,(long)hours,(long)minutes,(long)seconds];
NSLog(@"%@",dateContent);
NSMutableDictionary *dateInfo = [NSMutableDictionary dictionary];
[dateInfo setValue:day forKey:@"day"];
[dateInfo setValue:hour forKey:@"hour"];
[dateInfo setValue:minute forKey:@"minute"];
[dateInfo setValue:second forKey:@"second"];
return dateInfo;
}
//時間轉(zhuǎn)換
+ (NSString *)getTimeStrWithNum:(NSNumber *)time
{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[time longValue]/1000];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *string = [formatter stringFromDate:date];
return string;
}
//日期格式轉(zhuǎn)換
+ (NSString *)timeSpToTimeWithDate:(NSDate *)timesp
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy年MM月dd日"];
NSString *confromTimespStr = [formatter stringFromDate:timesp];
return confromTimespStr;
}
//圖片壓縮處理
+(NSData *)imageData:(UIImage *)image
{
NSData *data=UIImageJPEGRepresentation(image, 1.0);
if (data.length>100*1024)
{
if (data.length>5*1024*1024)
{//5M以及以上
data=UIImageJPEGRepresentation(image, 0.1);
}
else if (data.length>512*1024)
{//0.5M-1M
data=UIImageJPEGRepresentation(image, 0.5);
}
else if (data.length>200*1024)
{//0.25M-0.5M
data=UIImageJPEGRepresentation(image, 0.9);
}
data = UIImageJPEGRepresentation(image, 1.0);
}
return data;
}
//圖片大小設(shè)置
+ (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size
{
// 創(chuàng)建一個bitmap的context
// 并把它設(shè)置成為當(dāng)前正在使用的context
UIGraphicsBeginImageContext(size);
// 繪制改變大小的圖片
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 從當(dāng)前context中創(chuàng)建一個改變大小后的圖片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使當(dāng)前的context出堆棧
UIGraphicsEndImageContext();
// 返回新的改變大小后的圖片
return scaledImage;
}
/**
- 改變文本的字體顏色
- @param font 字體
- @param color 顏色
- @param totalString 總字符串
- @param subArray 需改變的字符串的數(shù)組
- @return
*/
+ (NSMutableAttributedString *)changeFontAndColor:(UIFont *)font
Color:(UIColor *)color
TotalString:(NSString *)totalString
SubStringArray:(NSArray *)subArray
{
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]
initWithString:totalString];
for (NSString *rangeStr in subArray)
{
NSRange range = [totalString rangeOfString:rangeStr
options:NSBackwardsSearch];
[attributedStr addAttribute:NSForegroundColorAttributeName
value:color
range:range];
[attributedStr addAttribute:NSFontAttributeName
value:font
range:range];
}
return attributedStr;
}
/**
文本中插入圖片
@param string 文字
@param imageNamed 圖片名稱
@param imageBounds 圖片大小位置
@param atIndex 插入的位置
@return 返回帶有圖片富文本
*/
+(NSMutableAttributedString *)lableInsertImageByString:(NSString *)string
needInsertImageNamed:(NSString *)imageNamed
imageBounds:(CGRect)imageBounds
insertAtIndex:(NSInteger)atIndex
{
//文本中放置圖片
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:string];
// 添加圖片
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
// 已支付圖片
attch.image = [UIImage imageNamed:imageNamed];
// 設(shè)置圖片大小
attch.bounds = imageBounds;//CGRectMake(0, -2, 22, 18);
// 創(chuàng)建帶有圖片的富文本
NSAttributedString *attStr = [NSAttributedString attributedStringWithAttachment:attch];
[attri insertAttributedString:attStr atIndex:atIndex];
return attri;
}
以上婉弹,就是今天所分享出來的東西,希望可以幫助到大家终吼,謝謝镀赌!