Swift: How to add and animate image view inline in label?

ぐ巨炮叔叔 提交于 2019-12-12 03:47:33

问题


In reference of Swift: make ellipsis continually retype in label? Tough label "animation"?, I am trying to animate little image views to jump in and scale at the foot of some text in a label to create the effect referenced in the above question.

To do this I need to add these image views inline in a label and then animate them using UIView.animateWithDuration and some scaling, etc.

I do not know how to add these images inline in the label so they appear in the same place on the label regardless of device - how can I add image views inline a label in Swift?


回答1:


You don't "add image views"; you add images as part of the label's text. Use an NSAttributedString. You will need an NSMutableAttributedString so that you can modify it. Make an NSTextAttachment and set its image to your image (needs to be the desired size beforehand, easy to do). Call NSAttributedString(attachment:) to make a second attributed string consisting of the inline image, and insert that new attributed string at the desired index into your original attributed string. Set the label's attributedText to that.



来源:https://stackoverflow.com/questions/38273972/swift-how-to-add-and-animate-image-view-inline-in-label

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!