神刀安全网

01-iOS总结:项目中的各种小坑汇总

这并不是我的第一篇,在这之前我已经在简书上写了5篇。。。。文章算不上,只是做个记录而已,由于篇幅太小,被我早早取消了亮相的资格,毕竟跟那些专职、兼职的作家和文字爱好者相比,人家动辄几千字的文稿,而我一个小小的iOS程序媛,上次写作文还是高考的事儿废话不多说,说也没墨水,还是赶紧上干货吧!

一、字符串转JSON

在网络请求时,如果服务端返回的是字符串,那么就需要我们自己封装一个类,来将请求下来的字符串转换成json对象。,从而存入模型中。

***注意: 字符串中如果含有一些特殊转意符(如/n、/t等),需要先对字符串进行处理。

示例代码如下:

+(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指定的矩形区域来填充图片     //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

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];
方式二
 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];

四、时间间隔一天

项目中的需求:控制弹窗弹出次数,要求每天弹出一次即可,写一个类,方便调用

示例代码如下:

+(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添加阴影效果无效

现在还是iOS小菜鸟一枚,道行尚浅,如有错误希望各位同行大神善意指出。如果觉得这只猴子写的不那么辣眼睛,那就动动手指给俺点个,小女子在此谢过 本文会不定期更新哦

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » 01-iOS总结:项目中的各种小坑汇总

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址