From 6f77983d8dc1b0d364c012ce9084bbc0a4942ffa Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Mon, 22 Feb 2021 16:35:56 -0800 Subject: [PATCH] Fix animated text attachment positioning --- Views/UIKit/AnimatedAttachmentLabel.swift | 2 -- Views/UIKit/AnimatedTextAttachment.swift | 10 +++++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Views/UIKit/AnimatedAttachmentLabel.swift b/Views/UIKit/AnimatedAttachmentLabel.swift index 70d9dcd..0857bd3 100644 --- a/Views/UIKit/AnimatedAttachmentLabel.swift +++ b/Views/UIKit/AnimatedAttachmentLabel.swift @@ -45,10 +45,8 @@ final class AnimatedAttachmentLabel: UILabel, EmojiInsertable { else { return } animatedAttachment.imageView.frame = imageBounds - animatedAttachment.imageView.image = animatedAttachment.image animatedAttachment.imageView.contentMode = .scaleAspectFit - animatedAttachment.imageView.center.y = center.y animatedAttachment.imageView.sd_setImage(with: animatedAttachment.imageURL) if animatedAttachment.imageView.superview != self { diff --git a/Views/UIKit/AnimatedTextAttachment.swift b/Views/UIKit/AnimatedTextAttachment.swift index 78eb56a..7e6841e 100644 --- a/Views/UIKit/AnimatedTextAttachment.swift +++ b/Views/UIKit/AnimatedTextAttachment.swift @@ -11,7 +11,15 @@ final class AnimatedTextAttachment: NSTextAttachment { override func image(forBounds imageBounds: CGRect, textContainer: NSTextContainer?, characterIndex charIndex: Int) -> UIImage? { - self.imageBounds = imageBounds + if let textContainer = textContainer, + let textContainerImageBounds = textContainer.layoutManager?.boundingRect( + forGlyphRange: NSRange(location: charIndex, length: 1), + in: textContainer), + textContainerImageBounds != .zero { + self.imageBounds = textContainerImageBounds + } else { + self.imageBounds = imageBounds + } return nil // rendered by AnimatingLayoutManager or AnimatedAttachmentLabel }