gpt4 book ai didi

ios - Swift 为文本字段影子代码创建扩展

转载 作者:行者123 更新时间:2023-11-28 14:19:06 25 4
gpt4 key购买 nike

我有多个文本字段,但所有文本字段都有相同的阴影效果,我需要为该阴影代码使用扩展,并在 viewdid 加载中很快使用它。

下面是我正在使用的代码

//MARK - Email TextField
email_textfield.borderStyle = .none
email_textfield.backgroundColor = UIColor.groupTableViewBackground // Use anycolor that give you a 2d look.

//To apply corner radius
email_textfield.layer.cornerRadius = email_textfield.frame.size.height / 2

//To apply border
email_textfield.layer.borderWidth = 0.25
email_textfield.layer.borderColor = UIColor.white.cgColor

//To apply Shadow
email_textfield.layer.shadowOpacity = 1
email_textfield.layer.shadowRadius = 1.0
email_textfield.layer.shadowOffset = CGSize.zero // Use any CGSize
email_textfield.layer.shadowColor = UIColor.lightGray.cgColor

//To apply padding
let paddingView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: email_textfield.frame.height))
email_textfield.leftView = paddingView
email_textfield.leftViewMode = UITextFieldViewMode.always

最佳答案

你可以像这样创建扩展

extension UITextField {

func applyCustomEffect() {
self.borderStyle = .none
self.backgroundColor = UIColor.groupTableViewBackground // Use anycolor that give you a 2d look.

//To apply corner radius
self.layer.cornerRadius = self.frame.size.height / 2

//To apply border
self.layer.borderWidth = 0.25
self.layer.borderColor = UIColor.white.cgColor

//To apply Shadow
self.layer.shadowOpacity = 1
self.layer.shadowRadius = 1.0
self.layer.shadowOffset = CGSize.zero // Use any CGSize
self.layer.shadowColor =
UIColor.lightGray.cgColor

self.layer.sublayerTransform = CATransform3DMakeTranslation(20, 0, 0)
}
}

然后像下面这样应用这个效果

email_textfield.applyCustomEffect()

关于ios - Swift 为文本字段影子代码创建扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52014371/

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