WFCUStreamingTextCell.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // TextCell.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/1.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUStreamingTextCell.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUUtilities.h"
  11. #import "AttributedLabel.h"
  12. #define TEXT_LABEL_TOP_PADDING 3
  13. #define TEXT_LABEL_BUTTOM_PADDING 5
  14. #define INDICTATORVIEW_HEGITH 18
  15. @interface WFCUStreamingTextCell () <AttributedLabelDelegate>
  16. @end
  17. @implementation WFCUStreamingTextCell
  18. + (CGSize)sizeForClientArea:(WFCUMessageModel *)msgModel withViewWidth:(CGFloat)width {
  19. NSString *text;
  20. BOOL generating = NO;
  21. if([msgModel.message.content isKindOfClass:[WFCCStreamingTextGeneratedMessageContent class]]) {
  22. WFCCStreamingTextGeneratedMessageContent *cnt = (WFCCStreamingTextGeneratedMessageContent *)msgModel.message.content;
  23. text = cnt.text;
  24. } else {
  25. WFCCStreamingTextGeneratingMessageContent *cnt = (WFCCStreamingTextGeneratingMessageContent *)msgModel.message.content;
  26. text = cnt.text;
  27. generating = YES;
  28. }
  29. CGSize size = [WFCUUtilities getTextDrawingSize:text font:[UIFont systemFontOfSize:18] constrainedSize:CGSizeMake(width, 8000)];
  30. size.height += TEXT_LABEL_TOP_PADDING + TEXT_LABEL_BUTTOM_PADDING;
  31. if (size.width < 40) {
  32. size.width += 4;
  33. if (size.width > 40) {
  34. size.width = 40;
  35. } else if (size.width < 24) {
  36. size.width = 24;
  37. }
  38. }
  39. if(generating) {
  40. size.height += INDICTATORVIEW_HEGITH;
  41. }
  42. return size;
  43. }
  44. - (void)setModel:(WFCUMessageModel *)model {
  45. [super setModel:model];
  46. NSString *text;
  47. BOOL generating = NO;
  48. if([model.message.content isKindOfClass:[WFCCStreamingTextGeneratedMessageContent class]]) {
  49. WFCCStreamingTextGeneratedMessageContent *cnt = (WFCCStreamingTextGeneratedMessageContent *)model.message.content;
  50. text = cnt.text;
  51. } else {
  52. WFCCStreamingTextGeneratingMessageContent *cnt = (WFCCStreamingTextGeneratingMessageContent *)model.message.content;
  53. text = cnt.text;
  54. generating = YES;
  55. }
  56. CGRect frame = self.contentArea.bounds;
  57. CGFloat indicatorHeight = 0;
  58. if(generating) {
  59. indicatorHeight = INDICTATORVIEW_HEGITH;
  60. }
  61. self.textLabel.frame = CGRectMake(0, TEXT_LABEL_TOP_PADDING, frame.size.width, frame.size.height - TEXT_LABEL_TOP_PADDING - TEXT_LABEL_BUTTOM_PADDING - indicatorHeight);
  62. self.textLabel.textAlignment = NSTextAlignmentLeft;
  63. [self.textLabel setText:text];
  64. if(generating) {
  65. CGRect textRect = self.textLabel.frame;
  66. self.indicatorView.frame = CGRectMake(0, textRect.origin.y + textRect.size.height + 4, 12, 12);
  67. self.indicatorView.hidden = NO;
  68. [self.indicatorView startAnimating];
  69. } else {
  70. self.indicatorView.frame = CGRectZero;
  71. self.indicatorView.hidden = YES;
  72. [self.indicatorView stopAnimating];
  73. }
  74. }
  75. - (UILabel *)textLabel {
  76. if (!_textLabel) {
  77. _textLabel = [[AttributedLabel alloc] init];
  78. ((AttributedLabel*)_textLabel).attributedLabelDelegate = self;
  79. _textLabel.numberOfLines = 0;
  80. _textLabel.font = [UIFont systemFontOfSize:18];
  81. _textLabel.userInteractionEnabled = YES;
  82. [self.contentArea addSubview:_textLabel];
  83. }
  84. return _textLabel;
  85. }
  86. - (UIActivityIndicatorView *)indicatorView {
  87. if(!_indicatorView) {
  88. _indicatorView = [[UIActivityIndicatorView alloc] init];
  89. [self.contentArea addSubview:_indicatorView];
  90. }
  91. return _indicatorView;;
  92. }
  93. #pragma mark - AttributedLabelDelegate
  94. - (void)didSelectUrl:(NSString *)urlString {
  95. [self.delegate didSelectUrl:self withModel:self.model withUrl:urlString];
  96. }
  97. - (void)didSelectPhoneNumber:(NSString *)phoneNumberString {
  98. [self.delegate didSelectPhoneNumber:self withModel:self.model withPhoneNumber:phoneNumberString];
  99. }
  100. @end