Bläddra i källkod

解决长文本显示和复制导致的崩溃

zwp 4 månader sedan
förälder
incheckning
0362649df9

+ 3 - 3
AIIM/Controller/chat/chetCell/ChatPopoverView.m

@@ -42,6 +42,8 @@ static CGFloat const kDefaultBorderWidth = 1.0;
         _popoverMargin = 10.0;
         
         [self commonInit];
+        
+        [self layoutViews];
     }
     return self;
 }
@@ -136,9 +138,7 @@ static CGFloat const kDefaultBorderWidth = 1.0;
 
 #pragma mark - Layout
 
-- (void)layoutSubviews {
-    [super layoutSubviews];
-    
+- (void)layoutViews {
     CGRect sourceRect = [self.sourceView convertRect:self.sourceView.bounds toView:nil];
     CGFloat currentArrowHeight = self.showArrow ? self.arrowHeight : 0;
     CGFloat popoverWidth = CGRectGetWidth(self.contentView.bounds) + 2 * self.cornerRadius;

+ 6 - 33
AIIM/Controller/chat/chetCell/chatCellView.m

@@ -1333,15 +1333,13 @@ static const CGFloat kMediaCornerRadius = 4.0f;
 }
 
 - (void)layoutTextContent {
-    CGSize textSize = [self calculateTextSize];
+    CGSize textSize = _messageModel.textLayoutSize;
     
     if (_messageModel.isSender) {
         [_textLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
             make.right.equalTo(_avatarImageView.mas_left).offset(-26);
             make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
             make.width.mas_equalTo(textSize.width);
-            make.height.mas_equalTo(textSize.height);
-            make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
         }];
         
         if (_messageModel.quoteMessage) {
@@ -1357,8 +1355,6 @@ static const CGFloat kMediaCornerRadius = 4.0f;
             make.left.equalTo(_avatarImageView.mas_right).offset(26);
             make.top.mas_equalTo(_avatarImageView.mas_top).offset(28);
             make.width.mas_equalTo(textSize.width);
-            make.height.mas_equalTo(textSize.height);
-            make.bottom.equalTo(self.contentView).offset(-kVerticalMargin).priorityHigh();
         }];
         
         if (_messageModel.quoteMessage) {
@@ -1549,22 +1545,6 @@ static const CGFloat kMediaCornerRadius = 4.0f;
     return CGSizeMake(adjustedWidth, adjustedHeight);
 }
 
-- (CGSize)calculateTextSize{
-    
-    NSString *text = _messageModel.content ?: @"";
-    
-    UILabel *label = [[UILabel alloc] init];
-    label.font = [UIFont systemFontOfSize:18.0]; // 设置字体大小
-    label.numberOfLines = 0; // 设置为0表示不限制行数
-    label.text = text; // 设置文本内容
-    label.preferredMaxLayoutWidth = kContentMaxWidth; // 设置固定宽度
-    CGSize size = [label sizeThatFits:CGSizeMake(kContentMaxWidth, CGFLOAT_MAX)];
-    
-    CGSize textSize = size;
-//    NSLog(@"文本宽度: %f, 文本高度: %f", textSize.width, textSize.height);
-    return textSize;
-}
-
 - (NSString *)formatTimeWithMilliseconds:(NSInteger)milliseconds{
     NSInteger seconds = milliseconds / 1000.0;
         
@@ -1793,12 +1773,12 @@ static const CGFloat kMediaCornerRadius = 4.0f;
     CGFloat contentHeight = 0;
     CGFloat horizontalMargin = 12.0f;
     CGFloat verticalMargin = 10.0f;
-    CGFloat maxWidth = kContentMaxWidth - 2 * horizontalMargin;
+    CGFloat maxWidth = kContentMaxWidth;
     // 计算内容高度
     switch (message.messageType) {
         case ChatMessageTypeText: {
             NSString *text = message.content ?: @"";
-            UIFont *font = [UIFont systemFontOfSize:17]; // 设置字体大小
+            UIFont *font = [UIFont systemFontOfSize:18]; // 设置字体大小
             CGSize maximumSize = CGSizeMake(maxWidth, CGFLOAT_MAX); // 设置最大宽度,高度设为CGFLOAT_MAX表示不限制高度
              
             NSDictionary *attributes = @{NSFontAttributeName: font};
@@ -1806,7 +1786,7 @@ static const CGFloat kMediaCornerRadius = 4.0f;
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:attributes
                                              context:nil];
-             
+            message.textLayoutSize = rect.size;
             CGFloat height = rect.size.height;
             
             contentHeight = height + 2 * verticalMargin;
@@ -1849,6 +1829,8 @@ static const CGFloat kMediaCornerRadius = 4.0f;
         case ChatMessageTypeDel:
             contentHeight = 20.f;
             break;
+        case ChatMessageTypeDFBUSY:
+            break;
     }
     
     // 总高度 = 内容高度 + 上下边距
@@ -1860,15 +1842,6 @@ static const CGFloat kMediaCornerRadius = 4.0f;
     return height;
 }
 
-+ (void)precalculateHeightForMessage:(ChatMessageModel *)message completion:(void(^)(CGFloat height))completion {
-    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-        CGFloat height = [self cellHeightForMessage:message];
-        dispatch_async(dispatch_get_main_queue(), ^{
-            if (completion) completion(height);
-        });
-    });
-}
-
 
 
 @end