iOS时间个性化设置设置
Posted 徒步天涯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS时间个性化设置设置相关的知识,希望对你有一定的参考价值。
现在在很多项目中,不会直接显示时间,很多时候都是显示“刚刚”,”XX分钟前”,等等字样,那么他们是怎么实现的呢 ? 1.新建一个NSDate的类目:NSDate+XMGExtension NSDate+XMGExtension.h 文件 #import <Foundation/Foundation.h> @interface NSDate (XMGExtension) -(NSDateComponents *)deltaFrom:(NSDate *)from; /** 是否是今年 */ -(BOOL)isThisYear; /** 是否是今天 */ -(BOOL)isToday; /** 是否是昨天 */ -(BOOL)isYesterday; @end NSDate+XMGExtension.m #import "NSDate+XMGExtension.h" @implementation NSDate (XMGExtension) -(NSDateComponents *)deltaFrom:(NSDate *)from{ // 日历 NSCalendar *calendar = [NSCalendar currentCalendar]; // 比较时间 NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth |NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ; return [calendar components:unit fromDate:from toDate:self options:0]; } -(BOOL)isThisYear{ // 日历 NSCalendar *calendar = [NSCalendar currentCalendar]; NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]]; NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self]; return nowYear == selfYear; } //-(BOOL)isToday{ // // 日历 // NSCalendar *calendar = [NSCalendar currentCalendar]; // // NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; // // NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]]; // // NSDateComponents *selfCmps = [calendar components:unit fromDate:self]; // // return nowCmps.year == selfCmps.year // && nowCmps.month == selfCmps.month // && nowCmps.day == selfCmps.day; // // //} -(BOOL)isToday{ NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd"; NSString *nowString = [fmt stringFromDate:[NSDate date]]; NSString *selfString = [fmt stringFromDate:self]; return [nowString isEqualToString:selfString]; } -(BOOL)isYesterday{ NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd"; NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]]; NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0]; return cmps.year == 0 && cmps.month == 0 && cmps.day == 1; } @end 在model中使用 create_time getter方法 XMGTopic.h文件 /** 发帖时间 */ @property(nonatomic,copy)NSString *create_time; XMGTopic.m文件 #import "XMGTopic.h" @implementation XMGTopic -(NSString *)create_time{ NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSDate *create = [fmt dateFromString:_create_time]; if (create.isThisYear) { //今年 if (create.isToday) { //今天 NSDateComponents *cmps = [[NSDate date] deltaFrom:create]; if (cmps.hour>=1) { //时间差距>=1小时 return [NSString stringWithFormat:@"%zd小时前",cmps.hour]; }else if(cmps.minute>=1){ //时间差距>=1分分钟 return [NSString stringWithFormat:@"%zd分钟前",cmps.minute]; }else{//时间差距<1分分钟 return @"刚刚"; } }else if(create.isYesterday){ //昨天 fmt.dateFormat= @"昨天 HH:mm"; return [fmt stringFromDate:create]; }else{ fmt.dateFormat= @"MM-dd HH:mm"; return [fmt stringFromDate:create]; } }else{ //不是今年 return _create_time; } } @end 在view中XMGTopicCell.m文件中 -(void)setTopic:(XMGTopic *)topic{ _topic = topic; topic.sina_v = arc4random_uniform(10)%2; // 新浪加V self.sinaVView.hidden = !topic.isSina_v; // 设置头像 [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:topic.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]]; // 设置昵称 self.nameLabel.text = topic.name; // 设置帖子的创建时间 self.createTimeLabel.text = topic.create_time; // 设置按钮文字 [self setupButtonTitle:self.dingButtton count:topic.ding placeholder:@"顶"]; [self setupButtonTitle:self.caiButton count:topic.cai placeholder:@"踩"]; [self setupButtonTitle:self.shareButton count:topic.repost placeholder:@"分享"]; [self setupButtonTitle:self.commentButton count:topic.comment placeholder:@"评论"]; }
具体效果
以上是关于iOS时间个性化设置设置的主要内容,如果未能解决你的问题,请参考以下文章
ios - Heroku 和 MongoDb 上的自定义解析服务器错误 3080:JSON 文本没有以数组或对象开头,并且允许未设置片段的选项