Merge pull request #7455 from vector-im/nimau/7413-pills-max-width

Fix: truncate pills if they are too long
This commit is contained in:
Nicolas Mauri
2023-03-30 10:12:47 +02:00
committed by GitHub
5 changed files with 20 additions and 7 deletions
+5 -4
View File
@@ -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))
@@ -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)),
@@ -125,6 +125,8 @@ class PillTextAttachment: NSTextAttachment {
width += 2 * sizes.horizontalMargin
}
width = min(width, data.maxWidth)
return CGSize(width: width,
height: sizes.pillHeight)
}
@@ -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 = .greatestFiniteMagnitude) {
self.pillType = pillType
self.items = items
self.isHighlighted = isHighlighted
self.alpha = alpha
self.font = font
self.maxWidth = maxWidth
}
// MARK: - Codable
@@ -126,6 +130,7 @@ struct PillTextAttachmentData: Codable {
} else {
throw PillTextAttachmentDataError.noFontData
}
maxWidth = .greatestFiniteMagnitude
}
func encode(to encoder: Encoder) throws {