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