【发布时间】:2021-10-18 01:52:53
【问题描述】:
您可能知道,如果您尝试为 UILabel 设置渐变背景,它会覆盖文本。 我想为 UILabels 编写一个扩展,以便放置渐变背景。 这是我的代码,但渐变放在一个正方形中,向下和向右移动到标签。我不明白为什么,因为我使用的是同一个框架
public extension UILabel {
func applyGradientToLabelAtAngle(_ angle : CGFloat, _ color1 : UIColor, _ color2 : UIColor){
let gradient = CAGradientLayer()
gradient.frame = self.frame
gradient.colors = [color1.cgColor, color2.cgColor]
gradient.calculatePoints(for: angle)
let index = self.layer.superlayer!.sublayers!.firstIndex(of: self.layer)!
self.layer.superlayer!.sublayers!.insert(gradient, at: index)
}
}
编辑:calculatePoints 函数计算位置,对问题不重要
【问题讨论】:
标签: ios swift user-interface gradient