mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Pill Performance Improvement part 3 (#1908)
* pill performance improvement * code improvement * better doc
This commit is contained in:
@@ -26,6 +26,7 @@ final class PillTextAttachment: NSTextAttachment {
|
||||
}
|
||||
|
||||
private(set) var pillData: PillTextAttachmentData!
|
||||
weak var view: UIView?
|
||||
private var lastBounds: CGRect?
|
||||
|
||||
func invalidateLastBounds() {
|
||||
@@ -33,14 +34,20 @@ final class PillTextAttachment: NSTextAttachment {
|
||||
}
|
||||
|
||||
override func attachmentBounds(for textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect {
|
||||
var rect: CGRect
|
||||
if let lastBounds {
|
||||
return lastBounds
|
||||
} else if let view {
|
||||
rect = CGRect(origin: .zero, size: view.sizeThatFits(UIView.layoutFittingExpandedSize))
|
||||
} else {
|
||||
rect = super.attachmentBounds(for: textContainer, proposedLineFragment: lineFrag, glyphPosition: position, characterIndex: charIndex)
|
||||
}
|
||||
var rect = super.attachmentBounds(for: textContainer, proposedLineFragment: lineFrag, glyphPosition: position, characterIndex: charIndex)
|
||||
|
||||
let fontData = pillData.fontData
|
||||
// Align the pill text vertically with the surrounding text.
|
||||
rect.origin.y = fontData.descender + (fontData.lineHeight - rect.height) / 2.0
|
||||
// Set the max width of the pill
|
||||
rect.size.width = min(rect.size.width, PillConstants.maxWidth)
|
||||
lastBounds = rect
|
||||
return rect
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user