gpt4 book ai didi

ios - 更改焦点上的文本字段

转载 作者:行者123 更新时间:2023-11-28 13:40:27 25 4
gpt4 key购买 nike

我实现了一个未划线的 UITextField - 因此我添加了两个函数,即 underlined()underlinedGrey

我工作正常,但我希望文本字段最初是 textfield.underlinedGrey(),一旦文本字段被聚焦:textfield.underlined()

我该如何实现?我已经浏览过网络,发现我必须添加一个事件监听器,但到目前为止没有任何效果。

编辑:

extension UITextField {

func clear () {
self.layer.sublayers?.forEach {
if $0 is CAGradientLayer {
$0.removeFromSuperlayer()
}
}
}

func underlined(){
clear()
let color = UIColor(red: 11/255, green: 95/255, blue: 244/255, alpha: 1).cgColor
let sndColor = UIColor(red: 124/255, green: 206/255, blue: 254/255, alpha: 1).cgColor

let gradient: CAGradientLayer = CAGradientLayer()
gradient.colors = [color, sndColor]
gradient.locations = [0.0, 0.8]
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 1, y: 0)
let width = CGFloat(3.0)
gradient.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)

self.layer.insertSublayer(gradient, at: 0)
self.layer.masksToBounds = true
}

func underlinedGrey(){
clear()
let color = UIColor(red: 112/255, green: 112/255, blue: 112/255, alpha: 0.4).cgColor

let border = CALayer()
let width = CGFloat(3.0)
border.borderColor = color
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}

最佳答案

遵循UITextFieldDelegate然后实现

func textFieldDidBeginEditing(_ textField: UITextField) {
textField.underlined()
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.underlinedGrey()
}

清除

extension UITextField {
func clear () {
self.layer.sublayers?.forEach {
if $0 is CAGradientLayer || $0.name == "MyLayer" {
$0.removeFromSuperlayer()
}
}
}

 let border = CALayer()
border.name = "MyLayer"
let width = CGFloat(3.0)
border.borderColor = color

关于ios - 更改焦点上的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56103743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com