提高開發(fā)效率用到的工具類(持續(xù)更新...)

?這段時間開發(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;
}

以上婉弹,就是今天所分享出來的東西,希望可以幫助到大家终吼,謝謝镀赌!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市际跪,隨后出現(xiàn)的幾起案子商佛,更是在濱河造成了極大的恐慌,老刑警劉巖垫卤,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異出牧,居然都是意外死亡穴肘,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門舔痕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來评抚,“玉大人,你說我怎么就攤上這事伯复】” “怎么了?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵啸如,是天一觀的道長侍匙。 經(jīng)常有香客問我,道長叮雳,這世上最難降的妖魔是什么想暗? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮帘不,結(jié)果婚禮上说莫,老公的妹妹穿的比我還像新娘。我一直安慰自己寞焙,他們只是感情好储狭,可當(dāng)我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著捣郊,像睡著了一般辽狈。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上呛牲,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天稻艰,我揣著相機與錄音,去河邊找鬼侈净。 笑死尊勿,一個胖子當(dāng)著我的面吹牛僧凤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播元扔,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼躯保,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了澎语?” 一聲冷哼從身側(cè)響起途事,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎擅羞,沒想到半個月后尸变,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡减俏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年召烂,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片娃承。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡奏夫,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出历筝,到底是詐尸還是另有隱情酗昼,我是刑警寧澤,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響萍悴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜碟婆,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望惕稻。 院中可真熱鬧竖共,春花似錦、人聲如沸俺祠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蜘渣。三九已至淌铐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蔫缸,已是汗流浹背腿准。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人吐葱。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓街望,卻偏偏與公主長得像,于是被迫代替她去往敵國和親弟跑。 傳聞我的和親對象是個殘疾皇子灾前,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明先生_X自主閱讀 15,979評論 3 119
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理孟辑,服務(wù)發(fā)現(xiàn)哎甲,斷路器,智...
    卡卡羅2017閱讀 134,651評論 18 139
  • 新的一周開始了饲嗽,踐行廖單已經(jīng)第三個禮拜了炭玫,總體來說還不錯吧,元正對英文繪本的興趣更濃些貌虾⊥碳樱《鵝媽媽童謠》真...
    元正媽媽閱讀 107評論 0 0
  • 少年人的情感最可怕。 你如果愛一個人酝惧,意味著你將舍棄一部分的自我榴鼎,因為分割出的那一部分的你要去包容一個人的缺點伯诬、他...
    縱我含情閱讀 444評論 0 0
  • 2015年7月月29日晚唇,計劃已久的旅行真正開始,來到了向往多時的自由海港城—廈門盗似。 從廣州出發(fā)哩陕,中轉(zhuǎn)深圳,偶遇暴雨...
    Danny平底鞋閱讀 427評論 0 0