QMTextModel.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // QMTextModel.m
  3. // IMSDK-OC
  4. //
  5. // Created by haochongfeng on 2018/1/23.
  6. // Copyright © 2018年 HCF. All rights reserved.
  7. //
  8. #import "QMTextModel.h"
  9. @implementation QMTextModel
  10. // 计算文本图片混合高度
  11. + (CGFloat)calcRobotHeight: (NSString *)htmlString {
  12. htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
  13. htmlString = [htmlString stringByReplacingOccurrencesOfString:@"</br>" withString:@"\n"];
  14. htmlString = [htmlString stringByReplacingOccurrencesOfString:@"</p>" withString:@"\n"];
  15. __block CGFloat height = 0;
  16. __block NSString *tempString = htmlString;
  17. NSRegularExpression *regularExpretion = [[NSRegularExpression alloc] initWithPattern:@"<[^>]*>" options:NSRegularExpressionCaseInsensitive error:nil];
  18. [regularExpretion enumerateMatchesInString:htmlString options:NSMatchingReportProgress range:NSMakeRange(0, [htmlString length]) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
  19. if (result.range.length != 0) {
  20. NSString *actionString = [NSString stringWithFormat:@"%@",[htmlString substringWithRange:result.range]];
  21. NSRange range = [tempString rangeOfString:actionString];
  22. // 判断知否包含图片资源
  23. NSArray *components = nil;
  24. if ([actionString rangeOfString:@"src=\""].location != NSNotFound) {
  25. components = [actionString componentsSeparatedByString:@"src=\""];
  26. }else if ([actionString rangeOfString:@"src="].location != NSNotFound) {
  27. components = [actionString componentsSeparatedByString:@"src="];
  28. }
  29. if (components.count >= 2) {
  30. // 文本高度计算
  31. height += [QMTextModel calcTextHeight:[tempString substringToIndex:range.location]];
  32. // 图片高度固定
  33. height += 100;
  34. tempString = [tempString substringFromIndex:range.location+range.length];
  35. }else {
  36. tempString = [tempString stringByReplacingCharactersInRange:range withString:@""];
  37. }
  38. }
  39. }];
  40. // 文本高度计算
  41. height += [QMTextModel calcTextHeight:tempString];
  42. return height;
  43. }
  44. // 计算文本高度
  45. + (CGFloat)calcTextHeight: (NSString *)text {
  46. MLEmojiLabel *tLabel = [MLEmojiLabel new];
  47. tLabel.numberOfLines = 0;
  48. tLabel.font = [UIFont systemFontOfSize:14.0f];
  49. tLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  50. tLabel.disableEmoji = NO;
  51. tLabel.disableThreeCommon = NO;
  52. tLabel.isNeedAtAndPoundSign = YES;
  53. tLabel.customEmojiRegex = @"\\:[^\\:]+\\:";
  54. tLabel.customEmojiPlistName = @"expressionImage.plist";
  55. tLabel.customEmojiBundleName = @"QMEmoticon.bundle";
  56. tLabel.text = text;
  57. CGSize size = [tLabel preferredSizeWithMaxWidth: [UIScreen mainScreen].bounds.size.width - 160];
  58. return size.height;
  59. }
  60. @end