NIMRobotContentConfig.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // NIMRobotContentConfig.m
  3. // NIMKit
  4. //
  5. // Created by chris on 2017/6/27.
  6. // Copyright © 2017年 NetEase. All rights reserved.
  7. //
  8. #import "NIMRobotContentConfig.h"
  9. #import "M80AttributedLabel+NIMKit.h"
  10. #import "NIMSessionRobotContentView.h"
  11. #import "UIView+NIM.h"
  12. #import "NIMKit.h"
  13. @interface NIMRobotContentConfig()
  14. @property (nonatomic,strong) M80AttributedLabel *label;
  15. @property (nonatomic,strong) NIMSessionRobotContentView *robotContentView;
  16. @property (nonatomic,strong) NIMMessageModel *robotModel;
  17. @end
  18. @implementation NIMRobotContentConfig
  19. - (CGSize)contentSize:(CGFloat)cellWidth message:(NIMMessage *)message
  20. {
  21. CGFloat msgBubbleMaxWidth = (cellWidth - 130);
  22. if ([self isFromRobot:message])
  23. {
  24. self.robotModel.message = message;
  25. self.robotContentView.nim_width = msgBubbleMaxWidth;
  26. [self.robotContentView setupRobot:self.robotModel];
  27. [self.robotContentView layoutIfNeeded];
  28. CGSize size = [self.robotContentView sizeThatFits:CGSizeMake(msgBubbleMaxWidth, CGFLOAT_MAX)];
  29. return size;
  30. }
  31. else
  32. {
  33. NSString *text = message.text;
  34. self.label.font = [[NIMKit sharedKit].config setting:message].font;
  35. [self.label nim_setText:text];
  36. CGFloat bubbleLeftToContent = 14;
  37. CGFloat contentRightToBubble = 14;
  38. CGFloat msgContentMaxWidth = (msgBubbleMaxWidth - contentRightToBubble - bubbleLeftToContent);
  39. return [self.label sizeThatFits:CGSizeMake(msgContentMaxWidth, CGFLOAT_MAX)];
  40. }
  41. }
  42. - (NSString *)cellContent:(NIMMessage *)message
  43. {
  44. if ([self isFromRobot:message])
  45. {
  46. return @"NIMSessionRobotContentView";
  47. }
  48. else
  49. {
  50. return @"NIMSessionTextContentView";
  51. }
  52. }
  53. - (UIEdgeInsets)contentViewInsets:(NIMMessage *)message
  54. {
  55. return [[NIMKit sharedKit].config setting:message].contentInsets;
  56. }
  57. #pragma mark - Private
  58. - (BOOL)isFromRobot:(NIMMessage *)message
  59. {
  60. NIMRobotObject *object = (NIMRobotObject *)message.messageObject;
  61. return object.isFromRobot;
  62. }
  63. - (M80AttributedLabel *)label
  64. {
  65. if (_label)
  66. {
  67. return _label;
  68. }
  69. _label = [[M80AttributedLabel alloc] initWithFrame:CGRectZero];
  70. return _label;
  71. }
  72. - (NIMSessionRobotContentView *)robotContentView
  73. {
  74. if (_robotContentView)
  75. {
  76. return _robotContentView;
  77. }
  78. _robotContentView = [[NIMSessionRobotContentView alloc] initSessionMessageContentView];
  79. return _robotContentView;
  80. }
  81. - (NIMMessageModel *)robotModel
  82. {
  83. if (_robotModel)
  84. {
  85. return _robotModel;
  86. }
  87. _robotModel = [[NIMMessageModel alloc] init];
  88. return _robotModel;
  89. }
  90. @end