Swift:如何在标签中内嵌图像视图并为其添加动画? [英] Swift: How to add and animate image view inline in label?

查看:72
本文介绍了Swift:如何在标签中内嵌图像视图并为其添加动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考,我试图为小的图像视图制作动画,以跳转并在标签中某些文本的底部缩放,以创建上述问题中引用的效果.

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.

为此,我需要在标签中内联添加这些图像视图,然后使用UIView.animateWithDuration和一些缩放比例等对其进行动画处理.

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.

我不知道如何在标签中内嵌这些图像,因此无论使用哪种设备,它们都将显示在标签上的同一位置-如何在Swift中内嵌标签中的图像视图?

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?

推荐答案

您不会添加图片视图";您将图像添加为标签文本的一部分.使用NSAttributedString.您将需要一个NSMutableAttributedString,以便您可以对其进行修改.制作一个NSTextAttachment并将其image设置为您的图像(需要预先设置为所需大小,易于操作).调用NSAttributedString(attachment:)来创建由内联图像组成的第二个属性字符串,然后将该新的属性字符串以所需的索引插入到您的原始属性字符串中.将标签的attributedText设置为此.

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.

这篇关于Swift:如何在标签中内嵌图像视图并为其添加动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆