Swift 如何部分更改 UILabel 的颜色

问题描述

enter image description here

我正在我的应用程序的导航栏上设计一个选项栏,我想实现与音乐应用程序相同的 UI,如您所见,当滑块滑动时,UILabel 正在部分改变其颜色,如何这个存档吗?我应该在 CALayer 上工作吗?

这是我目前所做的,只是不正确

UIView.animate(withDuration: 0.3,delay: 0,options: .curveEaSEOut,animations: {
        
            self.optionBarSlider.frame = self.selectedButton.frame
        }) { _ in
            self.selectedButton.setTitleColor(.black,for: .normal)
        } 

解决方法

您可以通过设置 attributedText 来更改您需要的部分的颜色

How do you set an Attributed Title Color for State in Swift