這并不是我的第一篇史侣,在這之前我已經(jīng)在簡書上寫了5篇拴泌。。惊橱。蚪腐。文章算不上,只是做個記錄而已李皇,由于篇幅太小削茁,被我早早取消了亮相的資格宙枷,畢竟跟那些專職掉房、兼職的作家和文字愛好者相比,人家動輒幾千字的文稿慰丛,而我一個小小的iOS程序媛卓囚,上次寫作文還是高考的事兒??廢話不多說,說也沒墨水诅病,還是趕緊上干貨吧哪亿!
目錄
1.字符串轉(zhuǎn)JSON
2.圖片拉伸
3.Label文字自適應(yīng)frame
4.時間間隔24小時(這個之前有錯誤,已更正)
5.兩個日期的 比較
6.UIView添加陰影效果無效
7.隱藏狀態(tài)欄
一贤笆、字符串轉(zhuǎn)JSON
在網(wǎng)絡(luò)請求時蝇棉,如果服務(wù)端返回的是字符串,那么就需要我們自己封裝一個類芥永,來將請求下來的字符串轉(zhuǎn)換成json對象篡殷。,從而存入模型中埋涧。
***注意: 字符串中如果含有一些特殊轉(zhuǎn)意符(如\n板辽、\t等),需要先對字符串進(jìn)行處理棘催。
示例代碼如下:
+(NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString{
if (jsonString == nil) {
return nil;
}// jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\\\" withString:@""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\r" withString:@""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\n" withString:@""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\s" withString:@""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\t" withString:@""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\v" withString:@""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\f" withString:@""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\b" withString:@""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\a" withString:@""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\e" withString:@""];
NSData * jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError * err;
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
if (err) {
YSXLog(@"json解析失敗:%@",err);
return nil;
}
return dic;}
二劲弦、圖片拉伸
示例代碼如下:
UIImageView *rightImagV = [[UIImageView alloc]init];
UIImage* img=[UIImage imageNamed:@"tu_text_Values"];//原圖
UIEdgeInsets edge=UIEdgeInsetsMake(5, myScalWidth(100), 5,myScalWidth(30));
//UIImageResizingModeStretch:拉伸模式,通過拉伸UIEdgeInsets指定的矩形區(qū)域來填充圖片
//UIImageResizingModeTile:平鋪模式醇坝,通過重復(fù)顯示UIEdgeInsets指定的矩形區(qū)域來填充圖
img= [img resizableImageWithCapInsets:edge resizingMode:UIImageResizingModeStretch];
rightImagV.image = img;
[rightImagV sizeToFit];
rightImagV.width = myScalWidth(73)+scoreL.width+myScalWidth(20);
rightImagV.x = SCREEN_WIDTH - myScalWidth(10)-rightImagV.width;
rightImagV.centerY = CGRectGetMidY(proV.frame);
[topView addSubview:rightImagV];
scoreL.x = myScalWidth(83);
scoreL.centerY = rightImagV.height*0.5;
[rightImagV addSubview:scoreL];
三邑跪、Label文字自適應(yīng)frame
Label文字自適應(yīng)frame,目前知道的有三種方式:
示例代碼如下:
方式一
推薦此方式呼猪,此方式能夠獲取高度画畅,實(shí)現(xiàn)自動換行、行距設(shè)置
UILabel * infoLab=[[UILabel alloc] init];// infoLab.text=self.infoText; infoLab.font=[UIFont systemFontOfSize:myScalFont(28)]; infoLab.textColor=RGB(102, 102, 102, 1); infoLab.numberOfLines=0; NSMutableAttributedString *infoStr = [HP_NString createAttributeStringWithText:self.infoText LineSpace:myScalHeight(22) andFont:infoLab.font andColor:infoLab.textColor]; infoLab.attributedText = infoStr; CGSize infoSize = [HP_NString sizeOfText:self.infoText withFont:infoLab.font andSize:CGSizeMake(bgView.valueOfW-myScalWidth(22)*2, 1000) andLineSpace:myScalHeight(22) andColor:infoLab.textColor]; infoLab.width=infoSize.width; infoLab.height=infoSize.height; infoLab.x=typeLab.valueOfX; infoLab.y=typeLab.valueOfBottomMargin+myScalHeight(24);
[self.view addSubview:infoLab];
方式二
detailLabel.text=correctM.remarkContent; NSMutableAttributedString *attStr = [HP_NString createAttributeStringWithText:detailLabel.text LineSpace:myScalHeight(14) andFont:[UIFont systemFontOfSize:myScalHeight(23)] andColor:RGB(0, 0, 0, 1)];detailLabel.attributedText=attStr;size = [HP_NString sizeOfText:detailLabel.text withFont:[UIFont systemFontOfSize:myScalFont(23)] andSize:CGSizeMake(detailbgView.valueOfW - myScalWidth(20)*2, SCREEN_HEIGHT) andLineSpace:myScalHeight(14) andColor:RGB(0, 0, 0, 1)];detailLabel.textColor=RGB(0, 0, 0, 1);detailLabel.font=[UIFontsystemFontOfSize:myScalHeight(23)];detailLabel.frame = CGRectMake(myScalWidth(20), myScalHeight(55), detailbgView.valueOfW - myScalWidth(20)*2, size.height); detailbgView.height=size.height+myScalHeight(100);
方式三
推薦
CGFloat detailInfoLabelX=CGRectGetMidX(questImageView.frame); CGFloat detailInfoLabelW=detailInfoView.width-detailInfoLabelX*2; UILabel * detailInfoLabel=[[UILabel alloc] init]; detailInfoLabel.numberOfLines=0; detailInfoLabel.text=@"啦啦啦啦啦啦啦啦啦啦啦啦啦啦啊啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啊啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啊啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦"; detailInfoLabel.textColor=RGB(102, 102, 102, 1); detailInfoLabel.font=[UIFont systemFontOfSize:myScalFont(20)]; CGSize detailSize=[detailInfoLabel.text sizeWithFont:detailInfoLabel.font constrainedToSize:CGSizeMake(detailInfoLabelW, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping]; detailInfoLabel.x=detailInfoLabelX; detailInfoLabel.y=0; detailInfoLabel.width=detailSize.width; detailInfoLabel.height=detailSize.height;
[detailInfoView addSubview:detailInfoLabel];
四郑叠、時間間隔24小時
??今天公眾號里有位大神給我留言指出這種做法只是間隔了24小時夜赵,并不能控制每天彈一次,我看了下確實(shí)寫的不嚴(yán)謹(jǐn)乡革,當(dāng)時上面說要求每天彈一次寇僧,想當(dāng)然的就覺得是24小時了摊腋,誤導(dǎo)了大家???這兒我暫時先改成間隔24小時,近期項(xiàng)目事情比較多嘁傀,還沒有時間完善判斷每天彈出的情況兴蒸,過幾天再更新哈
示例代碼如下:
+(void)jumpToVC:(UIViewController *)myVC withSaveParam:(NSString *)saveParam withSaveDate:(NSDate *)saveDate withNavigationController:(UINavigationController *)nav{
//判斷參數(shù)是否保存
if (saveParam.length>0 && saveParam != nil) {//Y
YSXLog(@"參數(shù)已保存");
}else{//N
//判斷時間是否保存
if (saveDate != nil) {//Y
//判斷是否超過24小時
if ([[NSDate date] timeIntervalSinceDate:saveDate]/3600 >24) {//超過24小時
[nav pushViewController:myVC animated:YES];
}else{
YSXLog(@"沒有超過24小時");
}
}else{//N跳轉(zhuǎn)
[nav pushViewController:myVC animated:YES];
}
}}
調(diào)用時,由于“所依賴的界面”還沒加載完细办,所以有時不能成功彈出橙凳,可以適當(dāng)延遲彈出時間1秒
示例代碼如下:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
EmailViewController * vc = [[EmailViewController alloc] init];
[YSXJumpToVC jumpToVC:vc withSaveParam:[YSXUserInfo sharedYSXUserInfo].addEmail withSaveDate:[YSXUserInfo sharedYSXUserInfo].addEmailDate withNavigationController:self.navigationController];
});
五、兩個日期的比較
從服務(wù)器以字符串的形式返回兩個時間笑撞,要求比較兩者的大小
示例代碼如下:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate * dt1 = [[NSDate alloc] init];
NSDate * dt2 = [[NSDate alloc] init];
//簽到拿到的時間
dt1 = [df dateFromString:[YSXUserInfo sharedYSXUserInfo].comentTimeStr1];
//實(shí)時獲取時間
dt2 = [df dateFromString:[YSXUserInfo sharedYSXUserInfo].comentTimeStr];
NSComparisonResult result = [dt1 compare:dt2];
if (result == NSOrderedDescending) {//dt1>dt2
redView.hidden = NO;
}else{
redView.hidden = YES;
}
- 當(dāng)dt1大于dt2時岛啸,結(jié)果為 NSOrderedDescending
- 當(dāng)dt1等于dt2時,結(jié)果為 NSOrderedSame
- 當(dāng)dt1小于dt2時茴肥,結(jié)果為NSOrderedAscending
六坚踩、UIView添加陰影效果無效
前幾天做功能的時候,給圓角化的view四周加陰影效果瓤狐,結(jié)果搞半天沒搞出來瞬铸,原來是我對view圓角化的時候,除了View.layer.cornerRadius的設(shè)置础锐,后面總是習(xí)慣地加上View.layer.masksToBounds = YES嗓节,剪裁了陰影當(dāng)然沒有了??這么粗心,程序媛當(dāng)?shù)轿疫@個地步也是醉了皆警。拦宣。。
七耀怜、隱藏狀態(tài)欄
一般情況下我們創(chuàng)建界面的時候系統(tǒng)會預(yù)留20px空白給頂部狀態(tài)欄恢着,但是這空白不好看呀,所以我們在對應(yīng)的控制器里viewDid方法里加上self.automaticallyAdjustsScrollViewInsets = NO财破,而[[UIApplication sharedApplication]setStatusBarHidden:YES
withAnimation:UIStatusBarAnimationFade];是將整個狀態(tài)欄給隱藏掉了掰派,用戶體驗(yàn)效果不好,這里并不提倡左痢。在我處理狀態(tài)欄的時候發(fā)現(xiàn)automaticallyAdjustsScrollViewInsets的方法不起作用靡羡,經(jīng)過上網(wǎng)查詢,最終解決了俊性,原來控制器里我將scrollView作為了第一視圖略步, 只要scrollView的第一視圖身份取消,automaticallyAdjustsScrollViewInsets方法就奏效了定页,具體什么原因?qū)е碌奶吮。椰F(xiàn)在還沒弄清楚??
好了,今天就記錄這么多吧典徊,本人現(xiàn)在還是iOS小菜鳥一枚杭煎,道行尚淺恩够,如有錯誤希望各位同行大神善意指出。如果覺得寫的不那么辣眼睛羡铲,那就動動手指給俺點(diǎn)個蜂桶,小女子在此謝過, 本文會不定期更新哦??