iOS 时间计算,label段落显示

1、时间计算,大多数我们从后台取回的时间字段都为NSNumber , 需求大多数都是以信息发布的时间都是以label显示出(多少分钟前发布等显示)

先把取到的NSNumber时间转换成字符串

/*** 时间nuber转时间字符串*/
+ (NSString *)numberWithConversion:(NSNumber *)number {
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    
    // 毫秒值转化为秒
    NSDate* dateTime = [NSDate dateWithTimeIntervalSince1970:[number doubleValue]/ 1000.0];

    return [formatter stringFromDate:dateTime];
}

计算发布时间

/**
 *  计算发布时间
 *
 *  @param theDate    发布时间
 *  @param servertime 服务器时间
 *
 *  @return 计算后的时间
 */
+ (NSString*)intervalSinceNow:(NSString*)theDate servertime:(NSString *)servertime {
    NSDateFormatter*date=[[NSDateFormatter alloc] init];
    [date setDateFormat:@"yyyyMMddHHmmss"];
    
    NSDate*dateStr=[date dateFromString:theDate];
    
    NSTimeInterval lateTime=[dateStr timeIntervalSince1970]*1;
    
    NSDate* dat = [date dateFromString:servertime];
    
    NSTimeInterval nowTime=[dat timeIntervalSince1970]*1;
    
    NSString*timeString=@"";
    
    NSTimeInterval showTime=nowTime-lateTime;
    if(showTime/3600<1) {//发表在一小时之内
        if(showTime/60<1) {
            timeString=@"刚刚发布";
        } else {
            timeString = [NSString stringWithFormat:@"%f", showTime/60];
            timeString = [timeString substringToIndex:timeString.length-7];
            timeString=[NSString stringWithFormat:@"%@分钟前发布", timeString];
        }
    } else if(showTime/3600>1&&showTime/86400<1) {//在一小时以上24小以内
        timeString = [NSString stringWithFormat:@"%f", showTime/3600];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@小时前发布", timeString];
    } else {//发表在24小时以上
        timeString = [NSString stringWithFormat:@"%f", showTime/86400];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@天前发布", timeString];
    }
    return timeString;
}

UIlabel段落的显示(显示结果)


iOS 时间计算,label段落显示_第1张图片
最终显示的结果

直接上代码:

//设置Lable首行缩进
    NSString * headerData = [YHHTools cancelSpaces:miaoReadModel.viewpoint];
    if (headerData.length > 196) {
        self.viewpointLable.text = [NSString stringWithFormat:@"%@....",[headerData substringToIndex:196]];
    }else {
        self.viewpointLable.text = headerData;
    }
    NSMutableAttributedString*text = [[NSMutableAttributedString alloc]initWithString:self.viewpointLable.text];
    NSMutableParagraphStyle*style = [[NSMutableParagraphStyle alloc]init];
//    style.headIndent = 0; //缩进
    style.firstLineHeadIndent = 30;
    style.lineSpacing=6;//行距
    style.alignment=NSTextAlignmentLeft;
    //需要设置的范围
    NSRange range = NSMakeRange(0,self.viewpointLable.text.length);
    [text addAttribute:NSParagraphStyleAttributeName value:style range:range];
    self.viewpointLable.attributedText= text;

当然我这些字段都是由后台传回,所以传回的文字经常会出现\r\n这样的标示会导致在计算cell高度的时候有写偏差就需要取消字符的换行

/**
 *  取消字符串的换行空格
 *
 *
 *  @return 取消后的字符串
 */
+ (NSString *)cancelSpaces:(NSString *)string {
    
    NSString * headerData = string;
    headerData = [headerData stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符
    headerData = [headerData stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    headerData = [headerData stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    
    return headerData;
}

你可能感兴趣的:(iOS 时间计算,label段落显示)