文本到 UITextView 的渐变颜色
Gradient color for text to UITextView
我需要将渐变颜色设置为带有白色背景颜色的 UITextView 颜色,就像在 Instagram 故事中一样。示例如下:
忽略以下代码中设置的约束,但概念应该是这样的:
containerView = GradientView.init()
self.view.addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
containerView.widthAnchor.constraint(equalToConstant: 220).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
containerView.backgroundColor = .white
textView = UITextView()
containerView.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 0).isActive = true
textView.rightAnchor.constraint(equalTo: containerView.rightAnchor, constant: 0).isActive = true
textView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0).isActive = true
textView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0).isActive = true
textView.text = "#SomeValue"
textView.font = UIFont.systemFont(ofSize: 22, weight: .bold)
containerView.layer.mask = textView.layer.sublayers?.last
其中 GradientView 是一个 UIView 子类,类似于:
class GradientView: UIView{
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
func addGradient(){
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = [UIColor.blue.cgColor,UIColor.red.cgColor,UIColor.green.cgColor]
gradient.startPoint = CGPoint(x: 0, y: 1)
gradient.endPoint = CGPoint(x: 1, y: 0)
gradient.locations = [0,0.5,1]
self.layer.addSublayer(gradient)
}
}
应该看起来像这样。也许你的渐变颜色会更好看。
我需要将渐变颜色设置为带有白色背景颜色的 UITextView 颜色,就像在 Instagram 故事中一样。示例如下:
忽略以下代码中设置的约束,但概念应该是这样的:
containerView = GradientView.init()
self.view.addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
containerView.widthAnchor.constraint(equalToConstant: 220).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
containerView.backgroundColor = .white
textView = UITextView()
containerView.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 0).isActive = true
textView.rightAnchor.constraint(equalTo: containerView.rightAnchor, constant: 0).isActive = true
textView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0).isActive = true
textView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0).isActive = true
textView.text = "#SomeValue"
textView.font = UIFont.systemFont(ofSize: 22, weight: .bold)
containerView.layer.mask = textView.layer.sublayers?.last
其中 GradientView 是一个 UIView 子类,类似于:
class GradientView: UIView{
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
func addGradient(){
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = [UIColor.blue.cgColor,UIColor.red.cgColor,UIColor.green.cgColor]
gradient.startPoint = CGPoint(x: 0, y: 1)
gradient.endPoint = CGPoint(x: 1, y: 0)
gradient.locations = [0,0.5,1]
self.layer.addSublayer(gradient)
}
}
应该看起来像这样。也许你的渐变颜色会更好看。