Skip to content

Commit

Permalink
Merge pull request #1624 from nextcloud/change-message-height-calcula…
Browse files Browse the repository at this point in the history
…tion-to-textview

Use UITextView to calculate the message height
  • Loading branch information
Ivansss authored Apr 16, 2024
2 parents a079f23 + 79b84fe commit df47403
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 6 additions & 11 deletions NextcloudTalk/BaseChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions NextcloudTalk/NextcloudTalk-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,6 @@
#import "AFNetworking.h"
#import "NCNotification.h"

#import "MessageBodyTextView.h"

#endif /* NextcloudTalk_Bridging_Header_h */

0 comments on commit df47403

Please sign in to comment.