MHTopic.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // MHTopic.m
  3. // MHDevelopExample
  4. //
  5. // Created by CoderMikeHe on 17/2/8.
  6. // Copyright © 2017年 CoderMikeHe. All rights reserved.
  7. //
  8. #import "MHTopic.h"
  9. @interface MHTopic ()
  10. /** 点赞数string */
  11. @property (nonatomic , copy) NSString * thumbNumsString;
  12. @end
  13. @implementation MHTopic
  14. + (NSDictionary *)modelContainerPropertyGenericClass {
  15. return @{@"CommentReplyResults" : [MHComment class]};
  16. }
  17. - (instancetype)init
  18. {
  19. self = [super init];
  20. if (self) {
  21. // 初始化
  22. _CommentReplyResults = [NSMutableArray array];
  23. // 由于这里只是评论一个视频
  24. }
  25. return self;
  26. }
  27. #pragma mark - 公共方法
  28. - (NSAttributedString *)attributedText
  29. {
  30. if (self.Content == nil) return nil;
  31. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.Content];
  32. [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, self.Content.length)];
  33. [attributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(#0A0A0A) range:NSMakeRange(0, self.Content.length)];
  34. NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
  35. paraStyle.alignment = NSTextAlignmentJustified;//两端对齐
  36. [paraStyle setLineSpacing:12];//行间距
  37. [attributedString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, self.Content.length)];
  38. [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleNone] range:NSMakeRange(0, self.Content.length)];
  39. return attributedString;
  40. }
  41. #pragma mark - Setter
  42. //- (void)setThumbNums:(long long)thumbNums
  43. //{
  44. // _thumbNums = thumbNums;
  45. //
  46. // self.thumbNumsString = [self _thumbNumsStringWithThumbNums:thumbNums];
  47. //}
  48. - (BOOL)mh_isThisYear:(NSDate *)date
  49. {
  50. NSCalendar *calendar = [NSCalendar currentCalendar];
  51. int unit = NSCalendarUnitYear;
  52. // 1.获得当前时间的年月日
  53. NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
  54. // 2.获得self的年月日
  55. NSDateComponents *selfCmps = [calendar components:unit fromDate:date];
  56. return nowCmps.year == selfCmps.year;
  57. }
  58. - (NSDateComponents *)mh_deltaWithNow:(NSDate *)date
  59. {
  60. NSCalendar *calendar = [NSCalendar currentCalendar];
  61. int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
  62. return [calendar components:unit fromDate:date toDate:[NSDate date] options:0];
  63. }
  64. #pragma mark - Getter
  65. - (NSString *)CreatedTime
  66. {
  67. // return @"今天";
  68. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  69. fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  70. //#warning CoderMikeHe: 真机调试的时候,必须加上这句
  71. fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_CH"];
  72. // 获得发布的具体时间
  73. NSDate *createDate = [fmt dateFromString:_CreatedTime];
  74. // 判断是否为今年
  75. if ([self mh_isThisYear:createDate]) {
  76. if (createDate.isToday) { // 今天
  77. NSDateComponents *cmps = [self mh_deltaWithNow:createDate];
  78. if (cmps.hour >= 1) { // 至少是1小时前发的
  79. return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
  80. } else if (cmps.minute >= 1) { // 1~59分钟之前发的
  81. return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
  82. } else { // 1分钟内发的
  83. return @"刚刚";
  84. }
  85. } else if (createDate.isYesterday) { // 昨天
  86. fmt.dateFormat = @"昨天 HH:mm";
  87. return [fmt stringFromDate:createDate];
  88. } else { // 至少是前天
  89. fmt.dateFormat = @"MM-dd HH:mm";
  90. return [fmt stringFromDate:createDate];
  91. }
  92. } else { // 非今年
  93. fmt.dateFormat = @"yyyy-MM-dd";
  94. return [fmt stringFromDate:createDate];
  95. }
  96. }
  97. #pragma mark - 私有方法
  98. // 点赞
  99. - (NSString *)_thumbNumsStringWithThumbNums:(long long)thumbNums
  100. {
  101. NSString *titleString = nil;
  102. if (thumbNums >= 10000) { // 上万
  103. CGFloat final = thumbNums / 10000.0;
  104. titleString = [NSString stringWithFormat:@"%.1f万", final];
  105. // 替换.0为空串
  106. titleString = [titleString stringByReplacingOccurrencesOfString:@".0" withString:@""];
  107. } else if (thumbNums > 0) { // 一万以内
  108. titleString = [NSString stringWithFormat:@"%lld", thumbNums];
  109. }
  110. return titleString;
  111. }
  112. @end