RQExercisExplainItemViewModel.m 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. NSLog(@"explain===%@",exerciseModel.ydtQuestionModel.explain);
  25. if (RQStringIsNotEmpty(exerciseModel.ydtQuestionModel.explain)) {
  26. NSString *readText = [[exerciseModel.ydtQuestionModel.explain stringByReplacingOccurrencesOfString:@"【" withString:@""] stringByReplacingOccurrencesOfString:@"】" withString:@""];
  27. NSArray *knameList = [self getKeyRangeList:exerciseModel.ydtQuestionModel.explain readText:readText];//获取关键字
  28. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] init];
  29. NSMutableAttributedString *analyseString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",readText]];
  30. [text appendAttributedString:analyseString];
  31. text.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
  32. text.yy_lineSpacing = 8;
  33. text.yy_color = RQ_MAIN_TEXT_COLOR_1;
  34. if(knameList){//替换关键字标红
  35. @try {
  36. for (NSDictionary *item in knameList) {
  37. NSArray *values = item.allValues.firstObject;
  38. NSString *replacementString = item.allKeys.firstObject;
  39. NSRange range = NSMakeRange([values[0] intValue], [values[1] intValue]);
  40. NSDictionary *attributes = @{ NSForegroundColorAttributeName: RGB_COLOR(244, 0, 47),NSFontAttributeName: [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]};
  41. [text replaceCharactersInRange:range withString:replacementString];
  42. [text setAttributes:attributes range:NSMakeRange(range.location, range.length)];
  43. }
  44. } @catch (NSException *exception) {
  45. NSLog(@"替换替换发生错误");
  46. }
  47. }
  48. self.explainString = text;
  49. CGFloat height = [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel];
  50. textHeight = height;
  51. }
  52. }
  53. self.rowHeight = headerHeight + textHeight + footerHeight;
  54. });
  55. }
  56. return self;
  57. }
  58. - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
  59. lb.attributedText = attributedStr;
  60. CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2) - (8 * 2), CGFLOAT_MAX);
  61. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
  62. lb.textLayout = layout;
  63. CGFloat introHeight = layout.textBoundingSize.height;
  64. return introHeight;
  65. }
  66. - (YYLabel *)yyLabel {
  67. if (!_yyLabel) {
  68. _yyLabel = [[YYLabel alloc] init];
  69. }
  70. return _yyLabel;
  71. }
  72. - (NSString *)itemClassName {
  73. return @"RQExercisExplainCell";
  74. }
  75. @end