RQExercisExplainItemViewModel.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // RQExercisExplainItemViewModel.m
  3. // JSJP
  4. //
  5. // Created by 张嵘 on 2022/7/3.
  6. //
  7. #import "RQExercisExplainItemViewModel.h"
  8. @interface RQExercisExplainItemViewModel ()
  9. @property (nonatomic, readwrite, strong) RQExerciseModel *exerciseModel;
  10. @property (nonatomic, readwrite, strong) YYLabel *yyLabel;
  11. @property (nonatomic, readwrite, strong) NSMutableAttributedString *explainString;
  12. @end
  13. @implementation RQExercisExplainItemViewModel
  14. - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
  15. if (self = [super init]) {
  16. @weakify(self)
  17. dispatch_async(dispatch_get_main_queue(), ^{
  18. @strongify(self)
  19. self.exerciseModel = exerciseModel;
  20. CGFloat headerHeight = 16.f + 26.f + 16.f;
  21. CGFloat textHeight = 0;
  22. CGFloat footerHeight = 16.f + 18.f + 16.f;
  23. if (!RQObjectIsNil(exerciseModel.ydtQuestionModel)) {
  24. if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.explain)) {
  25. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:exerciseModel.ydtQuestionModel.explain];
  26. text.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  27. text.yy_lineSpacing = 8;
  28. text.yy_color = RQ_MAIN_TEXT_COLOR_1;
  29. self.explainString = text;
  30. CGFloat height = [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel];
  31. textHeight = height;
  32. }
  33. }
  34. self.rowHeight = headerHeight + textHeight + footerHeight;
  35. });
  36. }
  37. return self;
  38. }
  39. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  40. lb.attributedText = attributedStr;
  41. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2), CGFLOAT_MAX);
  42. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  43. lb.textLayout = layout;
  44. CGFloat introHeight = layout.textBoundingSize.height;
  45. return introHeight;
  46. }
  47. - (YYLabel *)yyLabel {
  48. if (!_yyLabel) {
  49. _yyLabel = [[YYLabel alloc] init];
  50. }
  51. return _yyLabel;
  52. }
  53. - (NSString *)itemClassName {
  54. return @"RQExercisExplainCell";
  55. }
  56. @end