TUIChatShortcutMenuView.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // TUIChatShortcutMenuView.m
  3. // TUIChat
  4. //
  5. // Created by Tencent on 2023/6/29.
  6. // Copyright © 2024 Tencent. All rights reserved.
  7. #import "TUIChatShortcutMenuView.h"
  8. #import <TUICore/TUICore.h>
  9. #import <TUICore/TUIDefine.h>
  10. #import <TIMCommon/TIMDefine.h>
  11. @implementation TUIChatShortcutMenuCellData
  12. - (instancetype)init {
  13. self = [super init];
  14. if (self) {
  15. self.textColor = [UIColor tui_colorWithHex:@"#8F959E"];
  16. self.textFont = [UIFont systemFontOfSize:14];
  17. self.backgroundColor = [UIColor tui_colorWithHex:@"#F6F7F9"];
  18. self.cornerRadius = 16;
  19. self.borderColor = [UIColor tui_colorWithHex:@"#C5CBD4"];
  20. self.borderWidth = 1.0;
  21. }
  22. return self;
  23. }
  24. - (CGSize)calcSize {
  25. return [self calcMenuCellButtonSize:self.text];
  26. }
  27. - (CGSize)calcMenuCellButtonSize:(NSString *)title {
  28. CGFloat margin = 28;
  29. CGRect rect = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 32)
  30. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  31. attributes:@{ NSFontAttributeName : self.textFont}
  32. context:nil];
  33. return CGSizeMake(rect.size.width + margin, 32);
  34. }
  35. @end
  36. @interface TUIChatShortcutMenuCell()
  37. @end
  38. @implementation TUIChatShortcutMenuCell
  39. - (instancetype)initWithFrame:(CGRect)frame {
  40. self = [super initWithFrame:frame];
  41. if (self) {
  42. self.button = [UIButton new];
  43. [self addSubview:self.button];
  44. }
  45. return self;
  46. }
  47. - (void)fillWithData:(TUIChatShortcutMenuCellData *)cellData {
  48. self.cellData = cellData;
  49. [self.button setTitle:cellData.text forState:UIControlStateNormal];
  50. [self.button addTarget:cellData.target action:cellData.cselector forControlEvents:UIControlEventTouchUpInside];
  51. self.button.layer.cornerRadius = self.cellData.cornerRadius;
  52. self.button.titleLabel.font = self.cellData.textFont;
  53. self.button.backgroundColor = self.cellData.backgroundColor;
  54. [self.button setTitleColor:self.cellData.textColor forState:UIControlStateNormal];
  55. self.button.layer.borderWidth = self.cellData.borderWidth;
  56. self.button.layer.borderColor = self.cellData.borderColor.CGColor;
  57. [self updateConstraints];
  58. // tell constraints they need updating
  59. [self setNeedsUpdateConstraints];
  60. // update constraints now so we can animate the change
  61. [self updateConstraintsIfNeeded];
  62. [self layoutIfNeeded];
  63. }
  64. - (void)updateConstraints {
  65. [super updateConstraints];
  66. CGSize size = [self.cellData calcSize];
  67. [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.leading.mas_equalTo(12);
  69. make.centerY.mas_equalTo(self.mas_centerY);
  70. make.width.mas_equalTo(size.width);
  71. make.height.mas_equalTo(size.height);
  72. }];
  73. }
  74. @end
  75. @interface TUIChatShortcutMenuView() <UICollectionViewDelegate, UICollectionViewDataSource>
  76. @property (nonatomic, strong) UICollectionView *collectionView;
  77. @property (nonatomic, strong) NSMutableArray *dataSource;
  78. @end
  79. @implementation TUIChatShortcutMenuView
  80. - (instancetype)initWithDataSource:(NSArray *)source {
  81. self = [super init];
  82. if (self) {
  83. self.dataSource = [source mutableCopy];
  84. self.backgroundColor = [UIColor tui_colorWithHex:@"#EBF0F6"];
  85. [self addSubview:self.collectionView];
  86. }
  87. return self;
  88. }
  89. #pragma mark - Public
  90. - (void)updateFrame {
  91. self.mm_left(0).mm_top(0).mm_width(Screen_Width).mm_height(self.viewHeight > 0 ? self.viewHeight : 46);
  92. self.collectionView.mm_fill();
  93. }
  94. #pragma mark - Getter
  95. - (UICollectionView *)collectionView {
  96. if (!_collectionView) {
  97. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  98. [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
  99. _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
  100. _collectionView.delegate = self;
  101. _collectionView.dataSource = self;
  102. _collectionView.scrollEnabled = YES;
  103. _collectionView.backgroundColor = [UIColor clearColor];
  104. _collectionView.showsHorizontalScrollIndicator = NO;
  105. [_collectionView registerClass:[TUIChatShortcutMenuCell class] forCellWithReuseIdentifier:@"menuCell"];
  106. }
  107. return _collectionView;
  108. }
  109. #pragma mark - UICollectionViewDataSource & Delegate
  110. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  111. return 1;
  112. }
  113. - (NSInteger)collectionView:(UICollectionView *)collectionView
  114. numberOfItemsInSection:(NSInteger)section {
  115. return self.dataSource.count;
  116. }
  117. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
  118. cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  119. TUIChatShortcutMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"menuCell" forIndexPath:indexPath];
  120. TUIChatShortcutMenuCellData *cellData = self.dataSource[indexPath.row];
  121. [cell fillWithData:cellData];
  122. return cell;
  123. }
  124. - (CGSize)collectionView:(UICollectionView *)collectionView
  125. layout:(UICollectionViewLayout *)collectionViewLayout
  126. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  127. TUIChatShortcutMenuCellData *cellData = self.dataSource[indexPath.row];
  128. return CGSizeMake([cellData calcSize].width + 12, [cellData calcSize].height);
  129. }
  130. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
  131. layout:(UICollectionViewLayout *)collectionViewLayout
  132. insetForSectionAtIndex:(NSInteger)section {
  133. return UIEdgeInsetsMake(0, 0, 0, 0);
  134. }
  135. - (CGFloat)collectionView:(UICollectionView *)collectionView
  136. layout:(UICollectionViewLayout *)collectionViewLayout
  137. minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  138. return 0;
  139. }
  140. - (CGFloat)collectionView:(UICollectionView *)collectionView
  141. layout:(UICollectionViewLayout*)collectionViewLayout
  142. minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  143. return 0;
  144. }
  145. - (BOOL)collectionView:(UICollectionView *)collectionView
  146. shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  147. return YES;
  148. }
  149. @end