HomeCountView.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // HomeCountView.m
  3. // KulexiuForStudent
  4. //
  5. // Created by 王智 on 2024/11/27.
  6. //
  7. #import "HomeCountView.h"
  8. @interface HomeCountView ()
  9. @property (nonatomic, copy) HomeCountCallback callback;
  10. @property (weak, nonatomic) IBOutlet UILabel *courseCountLabel;
  11. @property (weak, nonatomic) IBOutlet UILabel *timeLabel;
  12. @end
  13. @implementation HomeCountView
  14. + (instancetype)sharedInstance {
  15. HomeCountView *view = [[[NSBundle mainBundle] loadNibNamed:@"HomeCountView" owner:nil options:nil] firstObject];
  16. return view;
  17. }
  18. - (void)configWithSource:(NSDictionary *)parm {
  19. // 时间显示
  20. NSInteger duration = [parm ks_integerValueForKey:@"recordTime"];
  21. NSString *timeDesc = [NSString getTimeStringChineseWithTime:duration];
  22. UIFont *numberFont = [UIFont fontWithName:@"DIN Alternate Bold" size:22.0f];
  23. UIFont *nomalFont = [UIFont systemFontOfSize:12.0f weight:UIFontWeightRegular];
  24. // 计算两种字体的 descender 和 ascender 差异
  25. CGFloat maxDescender = MIN(numberFont.capHeight, nomalFont.descender);
  26. NSInteger baselineOffsetNumber = (maxDescender - numberFont.descender)/ 4.0f;
  27. NSInteger baselineOffsetNormal = (maxDescender - nomalFont.descender) / 4.0f;
  28. NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
  29. style.minimumLineHeight = 26;
  30. style.maximumLineHeight = 26;
  31. NSMutableAttributedString *attrs = [[NSMutableAttributedString alloc] initWithString:timeDesc attributes:@{NSFontAttributeName : numberFont,NSBaselineOffsetAttributeName:@(baselineOffsetNumber), NSForegroundColorAttributeName : HexRGB(0x333333), NSParagraphStyleAttributeName : style}];
  32. if ([timeDesc containsString:@" 时 "]) {
  33. [attrs addAttributes:@{NSFontAttributeName : nomalFont, NSBaselineOffsetAttributeName:@(baselineOffsetNormal), NSForegroundColorAttributeName:HexRGB(0x777777),NSParagraphStyleAttributeName : style} range:[timeDesc rangeOfString:@" 时 "]];
  34. }
  35. [attrs addAttributes:@{NSFontAttributeName : nomalFont, NSBaselineOffsetAttributeName:@(baselineOffsetNormal), NSForegroundColorAttributeName:HexRGB(0x777777),NSParagraphStyleAttributeName : style} range:[timeDesc rangeOfString:@" 分 "]];
  36. [attrs addAttributes:@{NSFontAttributeName : nomalFont, NSBaselineOffsetAttributeName:@(baselineOffsetNormal), NSForegroundColorAttributeName:HexRGB(0x777777),NSParagraphStyleAttributeName : style} range:[timeDesc rangeOfString:@" 秒"]];
  37. self.timeLabel.attributedText = attrs;
  38. self.courseCountLabel.text = [NSString stringWithFormat:@"%zd/%zd", [parm ks_integerValueForKey:@"courseCount"], [parm ks_integerValueForKey:@"totalCourseCount"]];
  39. }
  40. - (void)countActionCallback:(HomeCountCallback)callback {
  41. if (callback) {
  42. self.callback = callback;
  43. }
  44. }
  45. - (IBAction)showPractice:(id)sender {
  46. if (self.callback) {
  47. self.callback(NO);
  48. }
  49. }
  50. - (IBAction)showCourseTable:(id)sender {
  51. if (self.callback) {
  52. self.callback(YES);
  53. }
  54. }
  55. + (CGFloat)getViewHeight {
  56. CGFloat imageWidth = (KPortraitWidth - 14 * 2 - 9) / 2;
  57. CGFloat height = imageWidth / 169 * 67 + 12;
  58. return height;
  59. }
  60. /*
  61. // Only override drawRect: if you perform custom drawing.
  62. // An empty implementation adversely affects performance during animation.
  63. - (void)drawRect:(CGRect)rect {
  64. // Drawing code
  65. }
  66. */
  67. @end