2021-02-22 07:10:34 +00:00
|
|
|
// Copyright © 2021 Metabolist. All rights reserved.
|
|
|
|
|
2021-02-22 23:59:33 +00:00
|
|
|
import SDWebImage
|
2021-02-22 07:10:34 +00:00
|
|
|
import UIKit
|
|
|
|
|
|
|
|
final class AnimatedTextAttachment: NSTextAttachment {
|
2021-02-23 02:51:31 +00:00
|
|
|
let imageView = SDAnimatedImageView()
|
|
|
|
private(set) var imageBounds: CGRect?
|
2021-02-22 07:10:34 +00:00
|
|
|
|
|
|
|
override func image(forBounds imageBounds: CGRect,
|
|
|
|
textContainer: NSTextContainer?,
|
|
|
|
characterIndex charIndex: Int) -> UIImage? {
|
2021-02-23 00:35:56 +00:00
|
|
|
if let textContainer = textContainer,
|
2021-02-23 02:51:31 +00:00
|
|
|
let layoutManager = textContainer.layoutManager,
|
2021-02-23 00:35:56 +00:00
|
|
|
let textContainerImageBounds = textContainer.layoutManager?.boundingRect(
|
2021-02-23 02:51:31 +00:00
|
|
|
forGlyphRange: NSRange(location: layoutManager.glyphIndexForCharacter(at: charIndex), length: 1),
|
2021-02-23 00:35:56 +00:00
|
|
|
in: textContainer),
|
|
|
|
textContainerImageBounds != .zero {
|
|
|
|
self.imageBounds = textContainerImageBounds
|
|
|
|
} else {
|
2021-02-23 02:51:31 +00:00
|
|
|
// Labels sometimes, but not always, end up in this path
|
2021-02-23 00:35:56 +00:00
|
|
|
self.imageBounds = imageBounds
|
|
|
|
}
|
2021-02-22 07:10:34 +00:00
|
|
|
|
|
|
|
return nil // rendered by AnimatingLayoutManager or AnimatedAttachmentLabel
|
|
|
|
}
|
|
|
|
}
|