iOS 時間計算,label段落顯示

1、時間計算,大多數(shù)我們從后臺取回的時間字段都為NSNumber , 需求大多數(shù)都是以信息發(fā)布的時間都是以label顯示出(多少分鐘前發(fā)布等顯示)

先把取到的NSNumber時間轉(zhuǎn)換成字符串

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

    return [formatter stringFromDate:dateTime];
}

計算發(fā)布時間

/**
 *  計算發(fā)布時間
 *
 *  @param theDate    發(fā)布時間
 *  @param servertime 服務(wù)器時間
 *
 *  @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) {//發(fā)表在一小時之內(nèi)
        if(showTime/60<1) {
            timeString=@"剛剛發(fā)布";
        } else {
            timeString = [NSString stringWithFormat:@"%f", showTime/60];
            timeString = [timeString substringToIndex:timeString.length-7];
            timeString=[NSString stringWithFormat:@"%@分鐘前發(fā)布", timeString];
        }
    } else if(showTime/3600>1&&showTime/86400<1) {//在一小時以上24小以內(nèi)
        timeString = [NSString stringWithFormat:@"%f", showTime/3600];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@小時前發(fā)布", timeString];
    } else {//發(fā)表在24小時以上
        timeString = [NSString stringWithFormat:@"%f", showTime/86400];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@天前發(fā)布", timeString];
    }
    return timeString;
}

UIlabel段落的顯示(顯示結(jié)果)


最終顯示的結(jié)果

直接上代碼:

//設(shè)置Lable首行縮進(jìn)
    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; //縮進(jìn)
    style.firstLineHeadIndent = 30;
    style.lineSpacing=6;//行距
    style.alignment=NSTextAlignmentLeft;
    //需要設(shè)置的范圍
    NSRange range = NSMakeRange(0,self.viewpointLable.text.length);
    [text addAttribute:NSParagraphStyleAttributeName value:style range:range];
    self.viewpointLable.attributedText= text;

當(dāng)然我這些字段都是由后臺傳回,所以傳回的文字經(jīng)常會出現(xiàn)\r\n這樣的標(biāo)示會導(dǎo)致在計算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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容