iOS總結:項目中的各種小坑匯總,ios總結坑匯總
一、字串轉JSON
在網路請求時,如果服務端返回的是字串,那麼就需要我們自己封裝一個類,將請求下來的字串轉換成json對象,從而存入模型中。
注意: 字串中如果含有一些特殊轉意符(如\n、\t等),需要先對字串進行處理。
範例程式碼如下:
+(NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString{if (jsonString == nil) { return nil;}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指定的矩形地區來填充圖片//UIImageResizingModeTile:平鋪模式,通過重複顯示UIEdgeInsets指定的矩形地區來填充圖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文字自適應frame
方式一
推薦此方式,此方式能夠擷取高度,實現自動換行、行距設定
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];
方式二
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];
四、時間間隔一天
項目中的需求:控制彈窗彈出次數,要求每天彈出一次即可,寫一個類,方便調用
+(void)jumpToVC:(UIViewController *)myVC withSaveParam:(NSString *)saveParam withSaveDate:(NSDate *)saveDate withNavigationController:(UINavigationController *)nav{//判斷參數是否儲存if (saveParam.length>0 && saveParam != nil) {//Y YSXLog(@"參數已儲存");}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跳轉 [nav pushViewController:myVC animated:YES]; }}}
調用時,由於“所依賴的介面”還沒載入完,所以有時不能成功彈出,可以適當延遲彈出時間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];});
五、兩個日期的比較
從伺服器以字串的形式返回兩個時間,要求比較兩者的大小
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]; //即時擷取時間 dt2 = [df dateFromString:[YSXUserInfo sharedYSXUserInfo].comentTimeStr]; NSComparisonResult result = [dt1 compare:dt2]; if (result == NSOrderedDescending) {//dt1>dt2 redView.hidden = NO; }else{ redView.hidden = YES; }//當dt1大於dt2時,結果為 NSOrderedDescending//當dt1等於dt2時,結果為 NSOrderedSame//當dt1小於dt2時,結果為NSOrderedAscending
六、UIView添加陰影製作效果無效
給圓角化的view四周加陰影製作效果,結果搞半天沒搞出來,原來是我對view圓角化的時候,除了View.layer.cornerRadius的設定,後面總是習慣地加上View.layer.masksToBounds = YES,剪裁了陰影當然沒有了。
七、隱藏狀態列
一般情況下我們建立介面的時候系統會預留20px空白給頂部狀態列,但是這空白不好看呀,所以我們在對應的控制器裡viewDidLoad方法裡加上self.automaticallyAdjustsScrollViewInsets = NO,而[[UIApplication sharedApplication]setStatusBarHidden:YES
withAnimation:UIStatusBarAnimationFade];是將整個狀態列給隱藏掉了,使用者體驗效果不好,這裡並不提倡。在我處理狀態列的時候發現automaticallyAdjustsScrollViewInsets的方法不起作用,經過上網查詢,最終解決了,原來控制器裡我將scrollView作為了第一視圖, 只要scrollView的第一視圖身份取消,automaticallyAdjustsScrollViewInsets方法就奏效了。
OK,今天先總結這幾點,錯誤的地方,希望大神多多指點!