From e37d8d5edfa555da618f21942f82648ed5897964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Sun, 28 Dec 2025 15:28:18 +0100 Subject: [PATCH] fix: Ensure message width is correctly calculated with quotes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcel Müller --- .../Chat/Chat views/MessageBodyTextView.m | 18 +++++++++++++ .../Chat/UnitBaseChatViewControllerTest.swift | 8 ------ .../Chat/UnitMessageBodyTextViewTest.swift | 25 +++++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 NextcloudTalkTests/Unit/Chat/UnitMessageBodyTextViewTest.swift diff --git a/NextcloudTalk/Chat/Chat views/MessageBodyTextView.m b/NextcloudTalk/Chat/Chat views/MessageBodyTextView.m index ad5910606..34d03f828 100644 --- a/NextcloudTalk/Chat/Chat views/MessageBodyTextView.m +++ b/NextcloudTalk/Chat/Chat views/MessageBodyTextView.m @@ -41,6 +41,24 @@ - (instancetype)init return self; } +- (CGSize)intrinsicContentSize { + CGSize superSize = [super intrinsicContentSize]; + + // When a paragraphStyle with firstLineHeadIndent/headIndent is used, the + // intrinsicContentSize might not be accurate and the last word/character is wrapped, + // due to the size being too small. In that case usedRectForTextContainer reports + // a non-zero x value, we add to the width of the intrinsicContentSize + if (superSize.width < UINT16_MAX) { + CGRect usedRect = [self.layoutManager usedRectForTextContainer:self.textContainer]; + + if (usedRect.origin.x > 0) { + return CGSizeMake(superSize.width + usedRect.origin.x, superSize.height); + } + } + + return superSize; +} + - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { return NO; diff --git a/NextcloudTalkTests/Unit/Chat/UnitBaseChatViewControllerTest.swift b/NextcloudTalkTests/Unit/Chat/UnitBaseChatViewControllerTest.swift index cb7250c68..83424e8f9 100644 --- a/NextcloudTalkTests/Unit/Chat/UnitBaseChatViewControllerTest.swift +++ b/NextcloudTalkTests/Unit/Chat/UnitBaseChatViewControllerTest.swift @@ -222,12 +222,4 @@ final class UnitBaseChatViewControllerTest: TestBaseRealm { XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 300), 171.0) } - /* - func testCellWithMarkdownQuoteHeight() throws { - // Normal chat message - testMessage.message = "> 1234567890123456789012345678901" - XCTAssertEqual(baseController.getCellHeight(for: testMessage, with: 372), 96.0) - } - */ - } diff --git a/NextcloudTalkTests/Unit/Chat/UnitMessageBodyTextViewTest.swift b/NextcloudTalkTests/Unit/Chat/UnitMessageBodyTextViewTest.swift new file mode 100644 index 000000000..0808151b7 --- /dev/null +++ b/NextcloudTalkTests/Unit/Chat/UnitMessageBodyTextViewTest.swift @@ -0,0 +1,25 @@ +// +// SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors +// SPDX-License-Identifier: GPL-3.0-or-later +// + +import XCTest +@testable import NextcloudTalk + +final class UnitMessageBodyTextViewTest: TestBaseRealm { + + func testCellWithMarkdownQuoteHeight() throws { + let activeAccount = NCDatabaseManager.sharedInstance().activeAccount() + let testMessage = NCChatMessage(dictionary: [:], andAccountId: activeAccount.accountId)! + + // Markdown message + testMessage.message = "> 1234567890123456789012345678" + testMessage.isMarkdownMessage = true + + let messageTextView = MessageBodyTextView() + messageTextView.attributedText = testMessage.parsedMarkdownForChat() + + XCTAssertEqual(messageTextView.intrinsicContentSize, CGSize(width: 259, height: 20)) + } + +}