123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- //
- // RQExerciseQuestionItemViewModel.m
- // SDJK
- //
- // Created by 张嵘 on 2021/8/16.
- //
- #import "RQExerciseQuestionItemViewModel.h"
- #import "XHWebImageAutoSize.h"
- @interface RQExerciseQuestionItemViewModel ()
- @property (nonatomic, readwrite, strong) RQExerciseModel *exerciseModel;
- @property (nonatomic, readwrite, strong) YYLabel *yyLabel;
- @property (nonatomic, readwrite, strong) NSString *typeString;
- @property (nonatomic, readwrite, strong) NSString *newString;
- @property (nonatomic, readwrite, strong) NSMutableAttributedString *qusetionString;
- @property (nonatomic, readwrite, assign) CGFloat labelHeight;
- @property (nonatomic, readwrite, assign) CGFloat imageHeight;
- @property (nonatomic, readwrite, strong) NSString *imageString;
- @property (nonatomic, readwrite, strong) NSString *videoString;
- @property (nonatomic, readwrite, strong) NSMutableAttributedString *spaceAttachment;
- @end
- @implementation RQExerciseQuestionItemViewModel
- - (instancetype)initWithRQExerciseModel:(RQExerciseModel *)exerciseModel {
- if (self = [super init]) {
- self.exerciseModel = exerciseModel;
- 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];
-
- NSMutableAttributedString *text = [NSMutableAttributedString new];
- NSMutableAttributedString *testText = [NSMutableAttributedString new];
-
- NSMutableAttributedString *typeAttachment = nil;
-
- QMUIButton *typeBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
- CGFloat btnHeight = (25.f / 17.f) * RQ_Exercise_Module.exerciseFontSize;
- CGFloat btnWeight = (60.f / 25.f) * btnHeight;
- CGFloat btnCornerRadius = btnHeight / 2.f;
- CGSize btnSize = CGSizeMake(btnWeight, btnHeight);
- typeBtn.size = btnSize;
- [typeBtn setTitle:self.typeString textColor:RQ_MAIN_TEXT_COLOR_WHITE Font:RQ_Exercise_Module.exerciseFontSize - 3 fotState:UIControlStateNormal];
- [typeBtn setBackgroundImage:[UIImage qmui_imageWithColor:RQ_MAIN_COLOR size:btnSize cornerRadiusArray:@[@(btnCornerRadius), @(btnCornerRadius), @0, @(btnCornerRadius)]] forState:UIControlStateNormal];
-
-
- typeAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:typeBtn
- contentMode:UIViewContentModeCenter
- attachmentSize:btnSize
- alignToFont:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]
- alignment:YYTextVerticalAlignmentCenter];
- [testText appendAttributedString:typeAttachment];
- [testText appendAttributedString:self.spaceAttachment];
-
-
- if (exerciseModel.is_new) {
- NSMutableAttributedString *newAttachment = nil;
- QMUIButton *newBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
- newBtn.size = btnSize;
- [newBtn setTitle:@"新规题" textColor:RQ_MAIN_TEXT_COLOR_WHITE Font:RQ_Exercise_Module.exerciseFontSize - 3 fotState:UIControlStateNormal];
- [newBtn setBackgroundImage:[UIImage qmui_imageWithColor:RQColorFromHexString(@"#9C5FED") size:btnSize cornerRadiusArray:@[@(btnCornerRadius), @(btnCornerRadius), @(btnCornerRadius), @(btnCornerRadius)]] forState:UIControlStateNormal];
- newAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:newBtn
- contentMode:UIViewContentModeCenter
- attachmentSize:btnSize
- alignToFont:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize]
- alignment:YYTextVerticalAlignmentCenter];
- [testText appendAttributedString:newAttachment];
- [testText appendAttributedString:self.spaceAttachment];
- }
-
-
- NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:questionStr];
- attrString.yy_font = [UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
- attrString.yy_color = RQ_MAIN_TEXT_COLOR_1;
-
- if (RQStringIsNotEmpty(exerciseModel.titlekeyword) && exerciseModel.isShowSkillExplanation) {
- [[[exerciseModel.titlekeyword componentsSeparatedByString:@"-"].rac_sequence.signal deliverOnMainThread] subscribeNext:^(NSString * _Nullable skillkeywordStr) {
- NSRange range = [questionStr rangeOfString:skillkeywordStr];
- [attrString yy_setColor:RQ_MAIN_TEXT_COLOR_RED range:range];
- [attrString yy_setFont:[UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize + 2] range:range];
- }];
- }
-
- // NSRange range = [string rangeOfString:@"《遵守实验室须知》"];
- // [attrString yy_setColor:[UIColor colorWithHexString:@"#19ADA9"] range:range];
- //
- // [attrString yy_setTextHighlightRange:range
- // color:[UIColor colorWithHexString:@"#19ADA9"]
- // backgroundColor:[UIColor blueColor]
- // tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
- // [_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
- // }];
- [testText appendAttributedString: attrString];
- [text appendAttributedString:testText];
- // [text appendAttributedString:[self padding]];
- text.yy_lineSpacing = 8;
-
-
- self.qusetionString = text;
- self.labelHeight = [self getMessageHeightWithAttributedStr:text andLabel:self.yyLabel];
-
- self.imageString = exerciseModel.image;
- self.videoString = @"";
-
- if (RQStringIsNotEmpty(self.imageString)) {
- NSString *cacheKeyStr = [[YYWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:self.imageString]];
- UIImage *cacheImage = [[YYImageCache sharedCache] getImageForKey:cacheKeyStr];
- CGSize imageSize;
- if (cacheImage) {
- imageSize = cacheImage.size;
- } else {
- imageSize = [UIImage getImageSizeWithURL:self.imageString];
- }
- if (imageSize.width == 0 || imageSize.height == 0) {
- self.imageHeight = (RQ_SCREEN_WIDTH - 32.f) * (175.f / 345.f);
- } else {
- self.imageHeight = (RQ_SCREEN_WIDTH - 32.f) * (imageSize.height / imageSize.width);
- }
- self.rowHeight = 16.f + self.labelHeight + 16.f + self.imageHeight + 16.f;
- } else {
- self.rowHeight = 16.f + self.labelHeight + 16.f;
- }
-
-
- // 预选Option
- // NSString *questionStr = [NSString stringWithFormat:@"%ld、%@",(long)exerciseModel.num + 1,exerciseModel.issue];
- // self.qusetionString = [[NSMutableAttributedString alloc] initWithString:questionStr];
- // NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- // [paragraphStyle setFirstLineHeadIndent:68];//首行缩进
- // [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:RQ_Exercise_Module.exerciseFontSize + 2] range:range];
- // [_qusetionString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:RQ_Exercise_Module.exerciseFontSize] range:targetRange];
- // [_qusetionString addAttribute:NSForegroundColorAttributeName value:RQ_MAIN_TEXT_COLOR_RED range:range];
- //
- // } else {
- // break;
- // }
- // }
- //
- // } completed:^{
- // NSMutableAttributedString *caculateStr = [[NSMutableAttributedString alloc] initWithAttributedString:_qusetionString];
- // [caculateStr appendAttributedString:[[NSMutableAttributedString alloc] initWithString:_typeString]];
- // CGFloat yyHeight = [caculateStr.string heightForFont:[UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize] 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]];
- // if (exerciseModel.is_new) {
- // [caculateStr appendAttributedString:[[NSMutableAttributedString alloc] initWithString:self.newString]];
- // }
- // CGFloat yyHeight = [caculateStr.string heightForFont:[UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize] 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:RQ_Exercise_Module.exerciseFontSize];
- 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;
- }
- - (NSString *)newString {
- return @"新规题";
- }
- /**
- * 获取lb的高度
- * @param mess lb.text
- * @param lb (YYLabel *)label
- * @return lb的高度
- */
- - (CGFloat)getMessageHeight:(NSString *)mess andLabel:(YYLabel *)lb {
- NSMutableAttributedString *introText = [[NSMutableAttributedString alloc] initWithString:mess];
- introText.yy_font = [UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize];
- introText.yy_lineSpacing = 8;
- lb.attributedText = introText;
- CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2), CGFLOAT_MAX);
- YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:introText];
- lb.textLayout = layout;
- CGFloat introHeight = layout.textBoundingSize.height;
- return introHeight;
- }
- - (CGFloat)getMessageHeightWithAttributedStr:(NSMutableAttributedString *)attributedStr andLabel:(YYLabel *)lb {
- lb.attributedText = attributedStr;
- CGSize introSize = CGSizeMake(RQ_SCREEN_WIDTH - (16 * 2), CGFLOAT_MAX);
- YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:attributedStr];
- lb.textLayout = layout;
- CGFloat introHeight = layout.textBoundingSize.height;
- return introHeight;
- }
- - (YYLabel *)yyLabel {
- if (!_yyLabel) {
- _yyLabel = [[YYLabel alloc] init];
- }
- return _yyLabel;
- }
- - (NSMutableAttributedString *)spaceAttachment {
- if (!_spaceAttachment) {
- UIView *spaceView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 4, 25)];
- _spaceAttachment = [NSMutableAttributedString yy_attachmentStringWithContent:spaceView contentMode:UIViewContentModeCenter attachmentSize:CGSizeMake(4, 25) alignToFont:[UIFont boldSystemFontOfSize:RQ_Exercise_Module.exerciseFontSize] alignment:YYTextVerticalAlignmentCenter];
- }
- return _spaceAttachment;
- }
- - (NSString *)itemClassName {
- return @"RQExerciseQuestionCell";
- }
- @end
|