From db3c399e8edfde7aee2d9dac77ce8549fbfe06c6 Mon Sep 17 00:00:00 2001 From: Nicolas Mauri Date: Thu, 23 Mar 2023 12:25:51 +0100 Subject: [PATCH 1/2] Fix: truncate pills if they are too long --- Riot/Modules/Pills/PillAttachmentView.swift | 9 +++++---- Riot/Modules/Pills/PillAttachmentViewProvider.swift | 8 ++++++-- Riot/Modules/Pills/PillTextAttachment.swift | 2 ++ Riot/Modules/Pills/PillTextAttachmentData.swift | 9 ++++++++- changelog.d/7413.bugfix | 1 + 5 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 changelog.d/7413.bugfix diff --git a/Riot/Modules/Pills/PillAttachmentView.swift b/Riot/Modules/Pills/PillAttachmentView.swift index 538b88a48..575808bd7 100644 --- a/Riot/Modules/Pills/PillAttachmentView.swift +++ b/Riot/Modules/Pills/PillAttachmentView.swift @@ -70,7 +70,6 @@ class PillAttachmentView: UIView { label.font = pillData.font label.textColor = pillData.isHighlighted ? theme.baseTextPrimaryColor : theme.textPrimaryColor label.translatesAutoresizingMaskIntoConstraints = false - label.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal) stack.addArrangedSubview(label) computedWidth += label.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: sizes.pillBackgroundHeight)).width @@ -146,10 +145,12 @@ class PillAttachmentView: UIView { computedWidth += 2 * sizes.horizontalMargin } + computedWidth = min(pillData.maxWidth, computedWidth) + let pillBackgroundView = UIView(frame: CGRect(x: 0, - y: sizes.verticalMargin, - width: computedWidth, - height: sizes.pillBackgroundHeight)) + y: sizes.verticalMargin, + width: computedWidth, + height: sizes.pillBackgroundHeight)) pillBackgroundView.vc_addSubViewMatchingParent(stack, withInsets: UIEdgeInsets(top: sizes.verticalMargin, left: leadingStackMargin, bottom: -sizes.verticalMargin, right: -sizes.horizontalMargin)) diff --git a/Riot/Modules/Pills/PillAttachmentViewProvider.swift b/Riot/Modules/Pills/PillAttachmentViewProvider.swift index ba03ef61a..ae02b019f 100644 --- a/Riot/Modules/Pills/PillAttachmentViewProvider.swift +++ b/Riot/Modules/Pills/PillAttachmentViewProvider.swift @@ -42,11 +42,15 @@ import UIKit return } - guard let pillData = textAttachment.data else { + guard var pillData = textAttachment.data else { MXLog.debug("[PillAttachmentViewProvider]: attachment misses pill data") return } - + + if let messageTextView { + pillData.maxWidth = messageTextView.bounds.width - 8 + } + let mainSession = AppDelegate.theDelegate().mxSessions.first as? MXSession let pillView = PillAttachmentView(frame: CGRect(origin: .zero, size: textAttachment.size(forFont: pillData.font)), diff --git a/Riot/Modules/Pills/PillTextAttachment.swift b/Riot/Modules/Pills/PillTextAttachment.swift index 5e46fe234..021e46ca4 100644 --- a/Riot/Modules/Pills/PillTextAttachment.swift +++ b/Riot/Modules/Pills/PillTextAttachment.swift @@ -125,6 +125,8 @@ class PillTextAttachment: NSTextAttachment { width += 2 * sizes.horizontalMargin } + width = min(width, data.maxWidth) + return CGSize(width: width, height: sizes.pillHeight) } diff --git a/Riot/Modules/Pills/PillTextAttachmentData.swift b/Riot/Modules/Pills/PillTextAttachmentData.swift index 99877444d..5a7ced6ba 100644 --- a/Riot/Modules/Pills/PillTextAttachmentData.swift +++ b/Riot/Modules/Pills/PillTextAttachmentData.swift @@ -72,6 +72,8 @@ struct PillTextAttachmentData: Codable { var alpha: CGFloat /// Font for the display name var font: UIFont + /// Max width + var maxWidth: CGFloat /// Helper for preferred text to display. var displayText: String { @@ -93,12 +95,14 @@ struct PillTextAttachmentData: Codable { items: [PillTextAttachmentItem], isHighlighted: Bool, alpha: CGFloat, - font: UIFont) { + font: UIFont, + maxWidth: CGFloat = CGFloat.greatestFiniteMagnitude) { self.pillType = pillType self.items = items self.isHighlighted = isHighlighted self.alpha = alpha self.font = font + self.maxWidth = maxWidth } // MARK: - Codable @@ -108,6 +112,7 @@ struct PillTextAttachmentData: Codable { case isHighlighted case alpha case font + case maxWidth } enum PillTextAttachmentDataError: Error { @@ -126,6 +131,7 @@ struct PillTextAttachmentData: Codable { } else { throw PillTextAttachmentDataError.noFontData } + maxWidth = try container.decode(CGFloat.self, forKey: .maxWidth) } func encode(to encoder: Encoder) throws { @@ -136,6 +142,7 @@ struct PillTextAttachmentData: Codable { try container.encode(alpha, forKey: .alpha) let fontData = try NSKeyedArchiver.archivedData(withRootObject: font, requiringSecureCoding: false) try container.encode(fontData, forKey: .font) + try container.encode(maxWidth, forKey: .maxWidth) } // MARK: - Pill representations diff --git a/changelog.d/7413.bugfix b/changelog.d/7413.bugfix new file mode 100644 index 000000000..6aabfb79b --- /dev/null +++ b/changelog.d/7413.bugfix @@ -0,0 +1 @@ +Long pills are now truncated. From 5e1997a6f3653fb40324ca2adf4bb4ec0594a4ce Mon Sep 17 00:00:00 2001 From: Nicolas Mauri Date: Thu, 30 Mar 2023 10:09:27 +0200 Subject: [PATCH 2/2] Fix: do not encode the maximum width of a Pill --- Riot/Modules/Pills/PillTextAttachmentData.swift | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Riot/Modules/Pills/PillTextAttachmentData.swift b/Riot/Modules/Pills/PillTextAttachmentData.swift index 5a7ced6ba..a119c941b 100644 --- a/Riot/Modules/Pills/PillTextAttachmentData.swift +++ b/Riot/Modules/Pills/PillTextAttachmentData.swift @@ -96,7 +96,7 @@ struct PillTextAttachmentData: Codable { isHighlighted: Bool, alpha: CGFloat, font: UIFont, - maxWidth: CGFloat = CGFloat.greatestFiniteMagnitude) { + maxWidth: CGFloat = .greatestFiniteMagnitude) { self.pillType = pillType self.items = items self.isHighlighted = isHighlighted @@ -112,7 +112,6 @@ struct PillTextAttachmentData: Codable { case isHighlighted case alpha case font - case maxWidth } enum PillTextAttachmentDataError: Error { @@ -131,7 +130,7 @@ struct PillTextAttachmentData: Codable { } else { throw PillTextAttachmentDataError.noFontData } - maxWidth = try container.decode(CGFloat.self, forKey: .maxWidth) + maxWidth = .greatestFiniteMagnitude } func encode(to encoder: Encoder) throws { @@ -142,7 +141,6 @@ struct PillTextAttachmentData: Codable { try container.encode(alpha, forKey: .alpha) let fontData = try NSKeyedArchiver.archivedData(withRootObject: font, requiringSecureCoding: false) try container.encode(fontData, forKey: .font) - try container.encode(maxWidth, forKey: .maxWidth) } // MARK: - Pill representations