RQExerciseQuestionItemViewModel.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // RQExerciseQuestionItemViewModel.m
  3. // SDJK
  4. //
  5. // Created by 张嵘 on 2021/8/16.
  6. //
  7. #import "RQExerciseQuestionItemViewModel.h"
  8. @interface RQExerciseQuestionItemViewModel ()
  9. @property (nonatomic, readwrite, strong) NSString *typeString;
  10. @property (nonatomic, readwrite, strong) NSMutableAttributedString *qusetionString;
  11. @property (nonatomic, readwrite, strong) NSString *imageString;
  12. @property (nonatomic, readwrite, strong) NSString *videoString;
  13. @end
  14. @implementation RQExerciseQuestionItemViewModel
  15. - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
  16. if (self = [super init]) {
  17. if ([exerciseModel.answer containsString:@"-"]) {
  18. self.typeString = @"多选题";
  19. } else if ([exerciseModel.answer containsString:@"√"] || [exerciseModel.answer containsString:@"×"]) {
  20. self.typeString = @"判断题";
  21. } else {
  22. self.typeString = @"单选题";
  23. }
  24. NSString *questionStr = [NSString stringWithFormat:@"%ld、%@",(long)exerciseModel.num + 1,exerciseModel.issue];
  25. self.qusetionString = [[NSMutableAttributedString alloc] initWithString:questionStr];
  26. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  27. [paragraphStyle setFirstLineHeadIndent:58];//首行缩进
  28. [paragraphStyle setLineSpacing:10];//调整行间距
  29. [_qusetionString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [questionStr length])];
  30. if (RQStringIsNotEmpty(exerciseModel.titlekeyword) && exerciseModel.isShowSkillExplanation) {
  31. [[[exerciseModel.titlekeyword componentsSeparatedByString:@"-"].rac_sequence.signal deliverOnMainThread] subscribeNext:^(NSString * _Nullable skillkeywordStr) {
  32. NSRange targetRange = NSMakeRange(0, questionStr.length);
  33. NSRange range = targetRange;
  34. while (true) {
  35. range = [questionStr rangeOfString:skillkeywordStr options:NSLiteralSearch range:targetRange];
  36. if (range.location != NSNotFound) {
  37. NSRange newRange = NSMakeRange(range.location - 1, range.length + 2);
  38. if (newRange.length + newRange.location > range.location + range.length) {
  39. NSInteger a = (newRange.length + newRange.location) - (range.location + range.length);
  40. newRange = NSMakeRange(newRange.location - a, newRange.length);
  41. }
  42. NSString *keywordStr = [questionStr substringWithRange:newRange];
  43. if ([keywordStr containsString:@"“"] || [keywordStr containsString:@"”"] || [keywordStr containsString:@"\""]) {
  44. range = newRange;
  45. }
  46. targetRange = NSMakeRange(NSMaxRange(range), questionStr.length-NSMaxRange(range));
  47. [_qusetionString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:19] range:range];
  48. [_qusetionString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:targetRange];
  49. [_qusetionString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:range];
  50. } else {
  51. break;
  52. }
  53. }
  54. } completed:^{
  55. // CGRect rect = [_qusetionString.string boundingRectWithSize:CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:19],NSParagraphStyleAttributeName:paragraphStyle} context:nil];
  56. // self.imageString = exerciseModel.image;
  57. // self.videoString = @"";
  58. //
  59. // if (RQStringIsEmpty(_imageString)) {
  60. // self.rowHeight = rect.size.height + (16 * 2);
  61. // } else {
  62. // self.rowHeight = rect.size.height + (RQ_SCREEN_WIDTH - 32) * 0.4 + (16 * 3);
  63. // }
  64. NSMutableAttributedString *caculateStr = [[NSMutableAttributedString alloc] initWithAttributedString:_qusetionString];
  65. [caculateStr appendAttributedString:[[NSMutableAttributedString alloc] initWithString:_typeString]];
  66. CGFloat yyHeight = [caculateStr.string heightForFont:[UIFont boldSystemFontOfSize:17] width:RQ_SCREEN_WIDTH - (16 * 2)];
  67. NSInteger lineNum = [self needLinesWithText:caculateStr.string width:RQ_SCREEN_WIDTH - (16 * 2)];
  68. CGFloat labelHeight = yyHeight + (lineNum > 1? lineNum - 1 : 0) * 10;
  69. self.imageString = exerciseModel.image;
  70. self.videoString = @"";
  71. if (RQStringIsEmpty(_imageString)) {
  72. self.rowHeight = labelHeight + (16 * 2);
  73. } else {
  74. self.rowHeight = labelHeight + (RQ_SCREEN_WIDTH - 32) * 0.4 + (16 * 3);
  75. }
  76. }];
  77. } else {
  78. NSMutableAttributedString *caculateStr = [[NSMutableAttributedString alloc] initWithAttributedString:_qusetionString];
  79. [caculateStr appendAttributedString:[[NSMutableAttributedString alloc] initWithString:_typeString]];
  80. CGFloat yyHeight = [caculateStr.string heightForFont:[UIFont boldSystemFontOfSize:17] width:RQ_SCREEN_WIDTH - (16 * 2)];
  81. NSInteger lineNum = [self needLinesWithText:caculateStr.string width:RQ_SCREEN_WIDTH - (16 * 2)];
  82. CGFloat labelHeight = yyHeight + (lineNum > 1? lineNum - 1 : 0) * 10;
  83. self.imageString = exerciseModel.image;
  84. self.videoString = @"";
  85. if (RQStringIsEmpty(_imageString)) {
  86. self.rowHeight = labelHeight + (16 * 2);
  87. } else {
  88. self.rowHeight = labelHeight + (RQ_SCREEN_WIDTH - 32) * 0.4 + (16 * 3);
  89. }
  90. }
  91. }
  92. return self;
  93. }
  94. /**
  95. 显示当前文字需要几行
  96. @param width 给定一个宽度
  97. @return 返回行数
  98. */
  99. - (NSInteger)needLinesWithText:(NSString *)textStr width:(CGFloat)width {
  100. //创建一个labe
  101. UILabel * label = [[UILabel alloc]init];
  102. //font和当前label保持一致
  103. label.font = [UIFont boldSystemFontOfSize:17];
  104. NSString * text = textStr;
  105. NSInteger sum = 0;
  106. //总行数受换行符影响,所以这里计算总行数,需要用换行符分隔这段文字,然后计算每段文字的行数,相加即是总行数。
  107. NSArray * splitText = [text componentsSeparatedByString:@"\n"];
  108. for (NSString * sText in splitText) {
  109. label.text = sText;
  110. //获取这段文字一行需要的size
  111. CGSize textSize = [label systemLayoutSizeFittingSize:CGSizeZero];
  112. //size.width/所需要的width 向上取整就是这段文字占的行数
  113. NSInteger lines = ceilf(textSize.width/width);
  114. //当是0的时候,说明这是换行,需要按一行算。
  115. lines = lines == 0?1:lines;
  116. sum += lines;
  117. }
  118. return sum;
  119. }
  120. @end