MORankCountdownTopView.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // MORankCountdownTopView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2025/9/12.
  6. //
  7. #import "MORankCountdownTopView.h"
  8. @interface MORankCountdownTopView ()
  9. @property (nonatomic, strong) UILabel *countDownLab;
  10. @end
  11. @implementation MORankCountdownTopView
  12. - (instancetype)init {
  13. if (self = [super init] ) {
  14. [self setupUI];
  15. }
  16. return self;
  17. }
  18. - (instancetype)initWithFrame:(CGRect)frame{
  19. self = [super initWithFrame:frame];
  20. if (self){
  21. [self setupUI];
  22. }
  23. return self;
  24. }
  25. - (void)setupUI{
  26. [self addSubview:self.countDownLab];
  27. [self.countDownLab mas_makeConstraints:^(MASConstraintMaker *make) {
  28. make.height.equalTo(@24.0);
  29. make.centerX.equalTo(self);
  30. make.width.equalTo(@166.0);
  31. make.centerY.equalTo(self);
  32. }];
  33. }
  34. - (UILabel *)countDownLab{
  35. if(!_countDownLab){
  36. _countDownLab = [[UILabel alloc] init];
  37. _countDownLab.backgroundColor = [MOTools colorWithHexString:@"#F3F4FA" alpha:1.0];
  38. _countDownLab.layer.cornerRadius = 12.0;
  39. _countDownLab.layer.masksToBounds = YES;
  40. _countDownLab.textColor = [MOTools colorWithHexString:@"#5C5E66" alpha:1.0];
  41. _countDownLab.font = [MOTextTools poppinsMediumFont:12.0];
  42. _countDownLab.text = @"Countdown: 00d 00h 00m";
  43. _countDownLab.textAlignment = NSTextAlignmentCenter;
  44. }
  45. return _countDownLab;
  46. }
  47. - (void)setEndTime:(double)endTime{
  48. _endTime = endTime;
  49. if(endTime <= 0){
  50. return;
  51. }
  52. [self toUpdateTheCountDownLab];
  53. }
  54. - (void)toUpdateTheCountDownLab{
  55. if(self.endTime <= 0){
  56. return;
  57. }
  58. NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
  59. NSTimeInterval interval = self.endTime / 1000.0 - currentTime;
  60. NSDateComponents *components = [[NSCalendar currentCalendar]
  61. components:(NSCalendarUnitSecond | NSCalendarUnitHour | NSCalendarUnitMinute)
  62. fromDate:[NSDate dateWithTimeIntervalSince1970:0]
  63. toDate:[NSDate dateWithTimeIntervalSince1970:interval]
  64. options:0];
  65. if(interval > 0){
  66. NSString *result = [NSString stringWithFormat:@"%02ldm %02lds",
  67. (long)components.minute,
  68. (long)components.second];
  69. NSString *timeStr = [NSString stringWithFormat:NSLocalString(@"C60011"),result];
  70. //富文本 宽度
  71. NSAttributedString *contentStr = [MOTextTools base_colorObjectStringWith:timeStr AndNameStr:result AndBaseColor:kBaseTextColor_2 AndNameColor:kBaseTextColor_1];
  72. self.countDownLab.attributedText = contentStr;
  73. CGFloat labWidth = [MOTools getWidthWithString:timeStr font:[MOTextTools poppinsMediumFont:12.0]] + 20.0;
  74. [self.countDownLab mas_remakeConstraints:^(MASConstraintMaker *make) {
  75. make.width.equalTo(@(labWidth));
  76. make.height.equalTo(@24.0);
  77. make.centerX.equalTo(self);
  78. make.centerY.equalTo(self);
  79. }];
  80. }
  81. else{
  82. self.timeEndBlock ? self.timeEndBlock() : nil;
  83. }
  84. }
  85. - (void)oneSecondPassed{
  86. [self toUpdateTheCountDownLab];
  87. }
  88. @end