首页 > 移动平台 > 详细

iOS时间个性化设置设置

时间:2016-06-02 23:28:26      阅读:192      评论:0      收藏:0      [点我收藏+]
现在在很多项目中,不会直接显示时间,很多时候都是显示“刚刚”,”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时间个性化设置设置

原文:http://www.cnblogs.com/qianLL/p/5554621.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!