其他分享
首页 > 其他分享> > UILabel时间显示相关

UILabel时间显示相关

作者:互联网

1.计算Label文字的高度
CGSize titleSize = [tagModel.title sizeWithAttributes:
										  @{ NSFontAttributeName: [UIFont systemFontOfSize:8.0f] } ];

titleLabel.frame = CGRectMake(0, 0, ceil(titleSize.width), ceil(titleSize.height));
2.获取当前时间相距的周,天,年

+ (NSString *)ddpGetExpectTimestamp:(NSInteger)year month:(NSUInteger)month day:(NSUInteger)day {
 
    ///< 当前时间
    NSDate *currentdata = [NSDate date];
 
    ///< NSCalendar -- 日历类,它提供了大部分的日期计算接口,并且允许您在NSDate和NSDateComponents之间转换
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    /*
    ///<  NSDateComponents:时间容器,一个包含了详细的年月日时分秒的容器。
    ///< 下例:获取指定日期的年,月,日
    NSDateComponents *comps = nil;
    comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentdata];
    NSLog(@"年 year = %ld",comps.year);
    NSLog(@"月 month = %ld",comps.month);
    NSLog(@"日 day = %ld",comps.day);*/
 
    
    NSDateComponents *datecomps = [[NSDateComponents alloc] init];
    [datecomps setYear:year?:0];
    [datecomps setMonth:month?:0];
    [datecomps setDay:day?:0];
    
    ///< dateByAddingComponents: 在参数date基础上,增加一个NSDateComponents类型的时间增量
    NSDate *calculatedate = [calendar dateByAddingComponents:datecomps toDate:currentdata options:0];
    
    ///< 打印推算时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSString *calculateStr = [formatter stringFromDate:calculatedate];
    
    DDLog(@"calculateStr 推算时间: %@",calculateStr );
 
    ///< 预期的推算时间
    NSString *result = [NSString stringWithFormat:@"%ld", (long)[calculatedate timeIntervalSince1970]];
    
    return result;

标签:datecomps,显示,NSDateComponents,month,comps,NSString,相关,UILabel,day
来源: https://blog.csdn.net/csdn_liuyw/article/details/89365771