From 79b84febaa5d707340c7c090ad0d2b290fe8ac75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Fri, 12 Apr 2024 16:53:28 +0200 Subject: [PATCH] Use UITextView to calculate the message height MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcel Müller --- NextcloudTalk/BaseChatViewController.swift | 17 ++++++----------- NextcloudTalk/NextcloudTalk-Bridging-Header.h | 2 ++ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/NextcloudTalk/BaseChatViewController.swift b/NextcloudTalk/BaseChatViewController.swift index 12575a165..dc1e4a9ed 100644 --- a/NextcloudTalk/BaseChatViewController.swift +++ b/NextcloudTalk/BaseChatViewController.swift @@ -2672,6 +2672,10 @@ import QuickLook return self.getCellHeight(for: message, with: width) } + lazy var textViewForSizing: UITextView = { + return MessageBodyTextView() + }() + // swiftlint:disable:next cyclomatic_complexity func getCellHeight(for message: NCChatMessage, with originalWidth: CGFloat) -> CGFloat { // Chat separators @@ -2695,18 +2699,9 @@ import QuickLook width -= kObjectShareMessageCellObjectTypeImageSize + 25 // 2*right(10) + left(5) } - let textStorage = NSTextStorage(attributedString: messageString) - let targetBounding = CGRect(x: 0, y: 0, width: width, height: CGFLOAT_MAX) - let container = NSTextContainer(size: targetBounding.size) - container.lineFragmentPadding = 0 - - let manager = NSLayoutManager() - manager.addTextContainer(container) - textStorage.addLayoutManager(manager) - - manager.glyphRange(forBoundingRect: targetBounding, in: container) - let bodyBounds = manager.usedRect(for: container) + self.textViewForSizing.attributedText = messageString + let bodyBounds = self.textViewForSizing.sizeThatFits(CGSize(width: width, height: CGFLOAT_MAX)) var height = ceil(bodyBounds.height) if (message.isGroupMessage && message.parent() == nil) || message.isSystemMessage() { diff --git a/NextcloudTalk/NextcloudTalk-Bridging-Header.h b/NextcloudTalk/NextcloudTalk-Bridging-Header.h index 6ec9fd891..1d8d7c44d 100644 --- a/NextcloudTalk/NextcloudTalk-Bridging-Header.h +++ b/NextcloudTalk/NextcloudTalk-Bridging-Header.h @@ -107,4 +107,6 @@ #import "AFNetworking.h" #import "NCNotification.h" +#import "MessageBodyTextView.h" + #endif /* NextcloudTalk_Bridging_Header_h */