UILabel文字上的渐变笔触 [英] Gradient Stroke On UILabel Text

查看:125
本文介绍了UILabel文字上的渐变笔触的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PS:我已经通过在targetView的图层上添加遮罩解决了该问题

我有一种情况,我必须使用渐变色来描边UIlabel文本,我找到了与描边色有关的每个链接,但是它们只是应用了简单的颜色,虽然不是那么复杂,但是我想实现的是渐变色描边UIlabel文字.到目前为止,我已经尝试过了.

I have a scenario in which I have to Stroke UIlabel text with Gradient color I have find every link related to stroke color but they are only applying simple colors which is not that complicated but What I wan to achieve is gradient color stroke on UIlabel Text. I have tried this so far.

 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
}

我将附上我已经实现以及想要实现的屏幕截图.

I am attaching screenshot which I have achieved and what I want to achieve.

到目前为止,我已经做到了.

我想实现的目标.

推荐答案

这是我解决此问题的方法.

Here is my solution to solve this problem.

  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
}

结果:

这篇关于UILabel文字上的渐变笔触的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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