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,今天先总结这几点,错误的地方,希望大神多多指点!😄😄😄