NTESSearchMessageContentCell.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // NTESSearchMessageContentCell.m
  3. // NIM
  4. //
  5. // Created by chris on 15/7/8.
  6. // Copyright (c) 2015年 Netease. All rights reserved.
  7. //
  8. #import "NTESSearchMessageContentCell.h"
  9. #import "NTESSearchLocalHistoryObject.h"
  10. #import "NTESSessionUtil.h"
  11. #import "UIView+NTES.h"
  12. #import "NIMAvatarImageView.h"
  13. //font
  14. CGFloat SearchCellTitleFontSize = 13.f;
  15. CGFloat SearchCellContentFontSize = 12.f;
  16. CGFloat SearchCellTimeFontSize = 12.f;
  17. //layout
  18. CGFloat SearchCellAvatarLeft = 15.f;
  19. CGFloat SearchCellAvatarAndTitleSpacing = 10.f;
  20. CGFloat SearchCellTitleTop = 10.f;
  21. CGFloat SearchCellTimeRight = 15.f;
  22. CGFloat SearchCellTimeTop = 10.f;
  23. CGFloat SearchCellContentTop = 30.f;
  24. CGFloat SearchCellContentBottom = 8.f;
  25. CGFloat SearchCellContentMaxWidth = 260.f;
  26. CGFloat SearchCellContentMinHeight = 15.f; //cell的高度是由文本高度决定的。防止没有文本的情况,导致cell的高度过小。
  27. @interface NTESSearchMessageContentCell()
  28. @property (nonatomic,strong) NIMAvatarImageView *avatar;
  29. @property (nonatomic,strong) UILabel *titleLabel;
  30. @property (nonatomic,strong) UILabel *contentLabel;
  31. @property (nonatomic,strong) UILabel *timeLabel;
  32. @property (nonatomic,strong) NTESSearchLocalHistoryObject *object;
  33. @end
  34. @implementation NTESSearchMessageContentCell
  35. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  36. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  37. if (self) {
  38. _avatar = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
  39. [self addSubview:_avatar];
  40. _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  41. _titleLabel.font = [UIFont systemFontOfSize:13.f];
  42. [self addSubview:_titleLabel];
  43. _contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  44. _contentLabel.font = [UIFont systemFontOfSize:12.f];
  45. _contentLabel.textColor = [UIColor grayColor];
  46. _contentLabel.numberOfLines = 0;
  47. [self addSubview:_contentLabel];
  48. _timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  49. _timeLabel.font = [UIFont systemFontOfSize:12.f];
  50. [self addSubview:_timeLabel];
  51. }
  52. return self;
  53. }
  54. - (void)refresh:(NTESSearchLocalHistoryObject *)object{
  55. self.object = object;
  56. NIMMessage *message = object.message;
  57. NIMKitInfo *info = [[NIMKit sharedKit] infoByUser:message.from option:nil];
  58. NSURL *avatarURL;
  59. if (info.avatarUrlString.length) {
  60. avatarURL = [NSURL URLWithString:info.avatarUrlString];
  61. }
  62. [self.avatar nim_setImageWithURL:avatarURL placeholderImage:info.avatarImage];
  63. self.titleLabel.text = info.showName;
  64. self.contentLabel.text = message.text;
  65. self.timeLabel.text = [NTESSessionUtil showTime:message.timestamp showDetail:YES];
  66. [self.titleLabel sizeToFit];
  67. self.contentLabel.size = [self.contentLabel sizeThatFits:CGSizeMake(SearchCellContentMaxWidth * UISreenWidthScale, CGFLOAT_MAX)];
  68. self.contentLabel.height = MAX(SearchCellContentMinHeight, self.contentLabel.height);
  69. [self.timeLabel sizeToFit];
  70. }
  71. - (void)layoutSubviews{
  72. [super layoutSubviews];
  73. self.avatar.top = SearchCellTitleTop;
  74. self.avatar.left = SearchCellAvatarLeft;
  75. self.titleLabel.left = self.avatar.right + SearchCellAvatarAndTitleSpacing;
  76. self.contentLabel.left = self.titleLabel.left;
  77. self.titleLabel.top = SearchCellTitleTop;
  78. self.contentLabel.bottom = self.height - SearchCellContentBottom;
  79. self.timeLabel.right = self.width - SearchCellTimeRight;
  80. self.timeLabel.top = SearchCellTimeTop;
  81. }
  82. @end