// // MHTopic.m // MHDevelopExample // // Created by CoderMikeHe on 17/2/8. // Copyright © 2017年 CoderMikeHe. All rights reserved. // #import "MHTopic.h" @interface MHTopic () /** 点赞数string */ @property (nonatomic , copy) NSString * thumbNumsString; @end @implementation MHTopic + (NSDictionary *)modelContainerPropertyGenericClass { return @{@"CommentReplyResults" : [MHComment class]}; } - (instancetype)init { self = [super init]; if (self) { // 初始化 _CommentReplyResults = [NSMutableArray array]; // 由于这里只是评论一个视频 } return self; } #pragma mark - 公共方法 - (NSAttributedString *)attributedText { if (self.Content == nil) return nil; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.Content]; [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, self.Content.length)]; [attributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(#0A0A0A) range:NSMakeRange(0, self.Content.length)]; NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init]; paraStyle.alignment = NSTextAlignmentJustified;//两端对齐 [paraStyle setLineSpacing:12];//行间距 [attributedString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, self.Content.length)]; [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleNone] range:NSMakeRange(0, self.Content.length)]; return attributedString; } #pragma mark - Setter //- (void)setThumbNums:(long long)thumbNums //{ // _thumbNums = thumbNums; // // self.thumbNumsString = [self _thumbNumsStringWithThumbNums:thumbNums]; //} - (BOOL)mh_isThisYear:(NSDate *)date { NSCalendar *calendar = [NSCalendar currentCalendar]; int unit = NSCalendarUnitYear; // 1.获得当前时间的年月日 NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]]; // 2.获得self的年月日 NSDateComponents *selfCmps = [calendar components:unit fromDate:date]; return nowCmps.year == selfCmps.year; } - (NSDateComponents *)mh_deltaWithNow:(NSDate *)date { NSCalendar *calendar = [NSCalendar currentCalendar]; int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; return [calendar components:unit fromDate:date toDate:[NSDate date] options:0]; } #pragma mark - Getter - (NSString *)CreatedTime { // return @"今天"; NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; //#warning CoderMikeHe: 真机调试的时候,必须加上这句 fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_CH"]; // 获得发布的具体时间 NSDate *createDate = [fmt dateFromString:_CreatedTime]; // 判断是否为今年 if ([self mh_isThisYear:createDate]) { if (createDate.isToday) { // 今天 NSDateComponents *cmps = [self mh_deltaWithNow:createDate]; if (cmps.hour >= 1) { // 至少是1小时前发的 return [NSString stringWithFormat:@"%zd小时前", cmps.hour]; } else if (cmps.minute >= 1) { // 1~59分钟之前发的 return [NSString stringWithFormat:@"%zd分钟前", cmps.minute]; } else { // 1分钟内发的 return @"刚刚"; } } else if (createDate.isYesterday) { // 昨天 fmt.dateFormat = @"昨天 HH:mm"; return [fmt stringFromDate:createDate]; } else { // 至少是前天 fmt.dateFormat = @"MM-dd HH:mm"; return [fmt stringFromDate:createDate]; } } else { // 非今年 fmt.dateFormat = @"yyyy-MM-dd"; return [fmt stringFromDate:createDate]; } } #pragma mark - 私有方法 // 点赞 - (NSString *)_thumbNumsStringWithThumbNums:(long long)thumbNums { NSString *titleString = nil; if (thumbNums >= 10000) { // 上万 CGFloat final = thumbNums / 10000.0; titleString = [NSString stringWithFormat:@"%.1f万", final]; // 替换.0为空串 titleString = [titleString stringByReplacingOccurrencesOfString:@".0" withString:@""]; } else if (thumbNums > 0) { // 一万以内 titleString = [NSString stringWithFormat:@"%lld", thumbNums]; } return titleString; } @end