I have a UIImage
that contains multiple image frames and a duration which was sliced together from an animated GIF file. If I load the image in a UIWebView
it will animate as expected, but I want to use a UITextView
instead. The obvious choice is to use TextKit with a custom NSLayoutManger
and many custom NSAttributedString
attributes to achieve various drawing effects. I have a single UITextView
that I'd like to give a NSAttributedString
that contains NSTextAttachment
s for all of the images I need to display inline. It's working fine except for when I load one of these animated UIImages
, they do not animate through their frames. Any thoughts on how to accomplish this? I've come up with a theoretical option:
- For each animated image add a custom defined attribute
- When layout manager goes to draw backgrounds, iterate this attribute to locate the positions of the attachment.
- Callback to the text view to create
UIImageView
s with the animated image, add as a subview, and callstartAnimating
I have yet to try this, but it's fairly hacky. I'm wondering if there's a better option.
In summary: I want to animate a UIImage
inside a UITextView
via NSTextAttachment
inside a NSAttributedString
without the necessity for extra UIImageView
objects. You can ignore the fact that this was previously done with a UIWebView
. UIImage
has properties (images
, duration
) for setting up animated frames, which I currently have configured. If I assign it to a UIImageView
it animates, if I set it as a NSTextAttachment
, no animation.
I would appreciate any feedback. Thanks!
NSTextAttachment
object and manually change theimage
property after a delay, do you see anything change? If so you could subclassNSTextAttachment
and recreate the animation behaviour. – PropraetorNSTextAttachment
with aCADisplayLink
that would analyze the animatedUIImage
and determine when it needed to update the image property of the text attachment. It seems only slightly less hacky than addingUIImageView
subviews to the text view. I will give it a try though and see the feasibility. – RemyGIF
asNSTextAttachment
to theUITextView
and it only stays static, like a regular image. – Optician