123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- //
- // RQExerciseQuestionItemViewModel.m
- // SDJK
- //
- // Created by 张嵘 on 2021/8/16.
- //
- #import "RQExerciseQuestionItemViewModel.h"
- @interface RQExerciseQuestionItemViewModel ()
- @property (nonatomic, readwrite, strong) NSString *typeString;
- @property (nonatomic, readwrite, strong) NSMutableAttributedString *qusetionString;
- @property (nonatomic, readwrite, strong) NSString *imageString;
- @property (nonatomic, readwrite, strong) NSString *videoString;
- @end
- @implementation RQExerciseQuestionItemViewModel
- - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
- if (self = [super init]) {
- if ([exerciseModel.answer containsString:@"-"]) {
- self.typeString = @"多选题";
- } else if ([exerciseModel.answer containsString:@"√"] || [exerciseModel.answer containsString:@"×"]) {
- self.typeString = @"判断题";
- } else {
- self.typeString = @"单选题";
- }
-
- NSString *questionStr = [NSString stringWithFormat:@"%ld、%@",(long)exerciseModel.num + 1,exerciseModel.issue];
- self.qusetionString = [[NSMutableAttributedString alloc] initWithString:questionStr];
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- [paragraphStyle setFirstLineHeadIndent:58];//首行缩进
- [paragraphStyle setLineSpacing:10];//调整行间距
- [_qusetionString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [questionStr length])];
-
- if (RQStringIsNotEmpty(exerciseModel.titlekeyword) && exerciseModel.isShowSkillExplanation) {
- [[[exerciseModel.titlekeyword componentsSeparatedByString:@"-"].rac_sequence.signal deliverOnMainThread] subscribeNext:^(NSString * _Nullable skillkeywordStr) {
- NSRange targetRange = NSMakeRange(0, questionStr.length);
- NSRange range = targetRange;
- while (true) {
- range = [questionStr rangeOfString:skillkeywordStr options:NSLiteralSearch range:targetRange];
-
- if (range.location != NSNotFound) {
- NSRange newRange = NSMakeRange(range.location - 1, range.length + 2);
- if (newRange.length + newRange.location > range.location + range.length) {
- NSInteger a = (newRange.length + newRange.location) - (range.location + range.length);
- newRange = NSMakeRange(newRange.location - a, newRange.length);
- }
- NSString *keywordStr = [questionStr substringWithRange:newRange];
- if ([keywordStr containsString:@"“"] || [keywordStr containsString:@"”"] || [keywordStr containsString:@"\""]) {
- range = newRange;
- }
- targetRange = NSMakeRange(NSMaxRange(range), questionStr.length-NSMaxRange(range));
-
- [_qusetionString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:19] range:range];
- [_qusetionString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:targetRange];
- [_qusetionString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:range];
-
- } else {
- break;
- }
- }
-
- } completed:^{
- // CGRect rect = [_qusetionString.string boundingRectWithSize:CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:19],NSParagraphStyleAttributeName:paragraphStyle} context:nil];
- // self.imageString = exerciseModel.image;
- // self.videoString = @"";
- //
- // if (RQStringIsEmpty(_imageString)) {
- // self.rowHeight = rect.size.height + (16 * 2);
- // } else {
- // self.rowHeight = rect.size.height + (RQ_SCREEN_WIDTH - 32) * 0.4 + (16 * 3);
- // }
- NSMutableAttributedString *caculateStr = [[NSMutableAttributedString alloc] initWithAttributedString:_qusetionString];
- [caculateStr appendAttributedString:[[NSMutableAttributedString alloc] initWithString:_typeString]];
- CGFloat yyHeight = [caculateStr.string heightForFont:[UIFont boldSystemFontOfSize:17] width:RQ_SCREEN_WIDTH - (16 * 2)];
- NSInteger lineNum = [self needLinesWithText:caculateStr.string width:RQ_SCREEN_WIDTH - (16 * 2)];
- CGFloat labelHeight = yyHeight + (lineNum > 1? lineNum - 1 : 0) * 10;
- self.imageString = exerciseModel.image;
- self.videoString = @"";
-
- if (RQStringIsEmpty(_imageString)) {
- self.rowHeight = labelHeight + (16 * 2);
- } else {
- self.rowHeight = labelHeight + (RQ_SCREEN_WIDTH - 32) * 0.4 + (16 * 3);
- }
- }];
- } else {
- NSMutableAttributedString *caculateStr = [[NSMutableAttributedString alloc] initWithAttributedString:_qusetionString];
- [caculateStr appendAttributedString:[[NSMutableAttributedString alloc] initWithString:_typeString]];
- CGFloat yyHeight = [caculateStr.string heightForFont:[UIFont boldSystemFontOfSize:17] width:RQ_SCREEN_WIDTH - (16 * 2)];
- NSInteger lineNum = [self needLinesWithText:caculateStr.string width:RQ_SCREEN_WIDTH - (16 * 2)];
- CGFloat labelHeight = yyHeight + (lineNum > 1? lineNum - 1 : 0) * 10;
- self.imageString = exerciseModel.image;
- self.videoString = @"";
-
- if (RQStringIsEmpty(_imageString)) {
- self.rowHeight = labelHeight + (16 * 2);
- } else {
- self.rowHeight = labelHeight + (RQ_SCREEN_WIDTH - 32) * 0.4 + (16 * 3);
- }
- }
-
-
- }
- return self;
- }
- /**
- 显示当前文字需要几行
- @param width 给定一个宽度
- @return 返回行数
- */
- - (NSInteger)needLinesWithText:(NSString *)textStr width:(CGFloat)width {
- //创建一个labe
- UILabel * label = [[UILabel alloc]init];
- //font和当前label保持一致
- label.font = [UIFont boldSystemFontOfSize:17];
- NSString * text = textStr;
- NSInteger sum = 0;
- //总行数受换行符影响,所以这里计算总行数,需要用换行符分隔这段文字,然后计算每段文字的行数,相加即是总行数。
- NSArray * splitText = [text componentsSeparatedByString:@"\n"];
- for (NSString * sText in splitText) {
- label.text = sText;
- //获取这段文字一行需要的size
- CGSize textSize = [label systemLayoutSizeFittingSize:CGSizeZero];
- //size.width/所需要的width 向上取整就是这段文字占的行数
- NSInteger lines = ceilf(textSize.width/width);
- //当是0的时候,说明这是换行,需要按一行算。
- lines = lines == 0?1:lines;
- sum += lines;
- }
- return sum;
- }
- @end
|