一个轻量级的UILabel子类,使用Swift实现逐字打印动画效果,使用超级简单
iOS自带逐字打印动画效果的UILabel
安装
手动
- 将CLTypingLabel.swift复制并粘贴到您的Xcode项目中。
CocoaPods
- 安装最新版本的CocoaPods:
gem install cocoapods
- 将以下行添加到您的Podfile中:
pod 'CLTypingLabel'
- 安装pod:
pod install
使用方法
- 将标签的类从UILabel更改为CLTypingLabel;
示例代码
@IBOutlet weak var myTypeWriterLabel: CLTypingLabel!
在运行时设置标签的text将自动触发动画
myTypeWriterLabel.text = "This is a demo of typing label animation..."
动画完成后,你也可以触发你需要的功能
myTypeWriterLabel.onTypingAnimationFinished = {
//code goes here
}
还支持attributedText
myTypeWriterLabel.attributedText = NSAttributedString(string: "This is an attributed text typing animation...", attributes: attr)
你可以在任何时候暂停打字动画
myTypeWriterLabel.pauseTyping() //this will pause the typing animation
myTypeWriterLabel.continueTyping() //this will continue paused typing animation
并且可以自定义每个字符之间的时间间隔
myTypeWriterLabel.charInterval = 0.08 //optional, default is 0.1
许可
此代码在MIT许可的条款和条件下分发。