如何解决UILabel文字上的渐变笔触
PS:我已经通过在targetView的图层上添加蒙版来解决了该问题
我有一种情况,我必须使用渐变色来描边UIlabel文本,我找到了与描边色有关的每个链接,但是它们只是应用了简单的颜色,虽然不是那么复杂,但是我想实现的是渐变色描边UIlabel文字。到目前为止,我已经尝试过了。
func createGradientLabel(_ targetView : UIView,letter : String,fontsize : Int,position : Int,_ startColor: UIColor = UIColor.init(named: "startColor")!,_ endColor: UIColor = UIColor.init(named: "endColor")!) {
gradientLayer = CAGradientLayer()
gradientLayer.frame = targetView.bounds
gradientLayer.colors = [startColor.cgColor,endColor.cgColor]
targetView.layer.addSublayer(gradientLayer)
gradientLayer.startPoint = CGPoint(x: 0.0,y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0,y: 0.5)
// Create a label and add it as a subview
let label = UILabel(frame: targetView.bounds)
let strokeTextAttributes = [
NSAttributedString.Key.strokeColor : UIColor.red,NSAttributedString.Key.foregroundColor : UIColor.clear,NSAttributedString.Key.strokeWidth : -4.0,NSAttributedString.Key.font : UIFont.setCustomFont(type: .Poppin_Reg,size: 30)]
as [NSAttributedString.Key : Any]
label.attributedText = NSMutableAttributedString(string: letter,attributes: strokeTextAttributes)
//label.text = letter
//label.font = UIFont.boldSystemFont(ofSize: CGFloat(fontsize))
targetView.addSubview(label)
label.layer.shadowColor = #colorLiteral(red: 1,green: 0.3294117647,blue: 0.2862745098,alpha: 1)
label.layer.shadowOffset = .init(width: 0,height: 7)
label.layer.shadowOpacity = 0.6
// doing this will stroke label with gradient
targetView.layer.mask = label.layer
}
我将附上我已经实现以及想要实现的屏幕截图。
到目前为止,我已经做到了。
我想实现的目标。
解决方法
您不能使用渐变笔触。最接近的是将渐变绘制为背景,并使用文本绘制对其进行 mask 。因此,您的问题归结为简单地将文本绘制为蒙版,包括字体和阴影。
,这是我解决这个问题的方法。
func createGradientLabel(_ targetView : UIView,letter : String,fontsize : Int,position : Int,_ startColor: UIColor = UIColor.init(named: "startColor")!,_ endColor: UIColor = UIColor.init(named: "endColor")!) {
gradientLayer = CAGradientLayer()
gradientLayer.frame = targetView.bounds
gradientLayer.colors = [startColor.cgColor,endColor.cgColor]
targetView.layer.addSublayer(gradientLayer)
gradientLayer.startPoint = CGPoint(x: 0.0,y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0,y: 0.5)
// Create a label and add it as a subview
let label = UILabel(frame: targetView.bounds)
let strokeTextAttributes = [
NSAttributedString.Key.strokeColor : UIColor.red,NSAttributedString.Key.foregroundColor : UIColor.clear,NSAttributedString.Key.strokeWidth : 8.0,NSAttributedString.Key.font : UIFont.setCustomFont(type: .Poppin_Bold,size: 30)]
as [NSAttributedString.Key : Any]
label.attributedText = NSMutableAttributedString(string: letter,attributes: strokeTextAttributes)
targetView.addSubview(label)
label.layer.shadowColor = #colorLiteral(red: 1,green: 0.3294117647,blue: 0.2862745098,alpha: 1)
label.layer.shadowOffset = .init(width: 0,height: 7)
label.layer.shadowOpacity = 0.8
//This Line is the key
targetView.layer.mask = label.layer
}
结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。