RQExerciseAnswerItemViewModel.m 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // RQExerciseAnswerItemViewModel.m
  3. // JSJP
  4. //
  5. // Created by 张嵘 on 2021/8/16.
  6. //
  7. #import "RQExerciseAnswerItemViewModel.h"
  8. @interface RQExerciseAnswerItemViewModel ()
  9. @property (nonatomic, readwrite, strong) NSString *answerString;
  10. @property (nonatomic, readwrite, strong) RQYDTJSModel *ydtJSModel;
  11. @property (nonatomic, readwrite, assign) CGFloat skillHeight;
  12. @property (nonatomic, readwrite, strong) YYLabel *yyLabel;
  13. @property (nonatomic, readwrite, strong) NSMutableAttributedString *skillString;
  14. @end
  15. @implementation RQExerciseAnswerItemViewModel
  16. - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
  17. if (self = [super init]) {
  18. @weakify(self)
  19. dispatch_async(dispatch_get_main_queue(), ^{
  20. @strongify(self)
  21. NSArray *allOptionsArr = @[@"A",@"B",@"C",@"D"];
  22. NSMutableArray *answerOptionArr = @[].mutableCopy;
  23. for (int i = 0; i < exerciseModel.ydtQuestionModel.AnswerTrue.length; i ++) {
  24. NSRange range;
  25. range.location = i;
  26. range.length = 1;
  27. NSString *indexStr = [exerciseModel.ydtQuestionModel.AnswerTrue substringWithRange:range];
  28. [answerOptionArr addObject:[allOptionsArr objectAtIndex:indexStr.integerValue - 1]];
  29. }
  30. self.ydtJSModel = [RQ_YDT_JS_Question_Module getQuestionJSWithQuestionId:exerciseModel.ydtQuestionModel.ID];
  31. self.answerString = [NSString stringWithFormat:@"答案 %@",[answerOptionArr.mutableCopy componentsJoinedByString:@" "]];
  32. __block CGFloat space = 0.f;
  33. if (RQObjectIsNil(self.ydtJSModel)) {
  34. self.skillHeight = 0.f;
  35. space = 0.f;
  36. } else {
  37. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:self.ydtJSModel.qb_read_analyse];
  38. text.yy_font = [UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  39. text.yy_lineSpacing = 8;
  40. text.yy_color = RQ_MAIN_TEXT_COLOR_1;
  41. self.skillString = text;
  42. NSInteger line = RQ_COMMON_MANAGER.APP_SWITCH? 0 : ((RQ_VIP_Module.isSubject1Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectOne)? 0 : ((RQ_VIP_Module.isSubject4Vip && RQ_YDTQuestion_Module.subject == RQHomePageSubjectType_SubjectFour)? 0 : 1));
  43. CGFloat height = (line == 0)? [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel] : RQ_FIT_HORIZONTAL(34.f);
  44. self.skillHeight = height;
  45. space = 6.f;
  46. }
  47. self.rowHeight = [_answerString heightForFont:[UIFont boldSystemFontOfSize:19] width:RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2)] + (8 * 2) + (16 * 2) + space + self.skillHeight;
  48. });
  49. }
  50. return self;
  51. }
  52. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  53. lb.attributedText = attributedStr;
  54. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2), CGFLOAT_MAX);
  55. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  56. lb.textLayout = layout;
  57. CGFloat introHeight = layout.textBoundingSize.height;
  58. return introHeight;
  59. }
  60. - (YYLabel *)yyLabel {
  61. if (!_yyLabel) {
  62. _yyLabel = [[YYLabel alloc] init];
  63. }
  64. return _yyLabel;
  65. }
  66. - (NSString *)itemClassName {
  67. return @"RQExerciseAnswerCell";
  68. }
  69. @end