iOS UITextField swift下划线样式

iOS UITextField swift下划线样式,ios,swift,uitextfield,Ios,Swift,Uitextfield,我已经添加了这个用户界面登录的图像,希望您能看到。请注意,文本字段是透明的,但底部的行除外。我应该输入什么代码来获得这种影响?我可以在“用户定义的运行时属性”中输入必要的信息吗? 创建UITextField的子类,如下所示,只需在情节提要中将该类设置为UITextField Swift 5支持@IBInspectable import UIKit class HSUnderLineTextField: UITextField , UITextFieldDelegate { let b

我已经添加了这个用户界面登录的图像,希望您能看到。请注意,文本字段是透明的,但底部的行除外。我应该输入什么代码来获得这种影响?我可以在“用户定义的运行时属性”中输入必要的信息吗?

创建
UITextField
的子类,如下所示,只需在情节提要中将该类设置为
UITextField

Swift 5支持@IBInspectable

import UIKit

class HSUnderLineTextField: UITextField , UITextFieldDelegate {

    let border = CALayer()

    @IBInspectable open var lineColor : UIColor = UIColor.black {
        didSet{
            border.borderColor = lineColor.cgColor
        }
    }

    @IBInspectable open var selectedLineColor : UIColor = UIColor.black {
        didSet{
        }
    }


    @IBInspectable open var lineHeight : CGFloat = CGFloat(1.0) {
        didSet{
            border.frame = CGRect(x: 0, y: self.frame.size.height - lineHeight, width:  self.frame.size.width, height: self.frame.size.height)
        }
    }

    required init?(coder aDecoder: (NSCoder?)) {
        super.init(coder: aDecoder!)
        self.delegate=self;
        border.borderColor = lineColor.cgColor
        self.attributedPlaceholder = NSAttributedString(string: self.placeholder ?? "",
                                                               attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])


        border.frame = CGRect(x: 0, y: self.frame.size.height - lineHeight, width:  self.frame.size.width, height: self.frame.size.height)
        border.borderWidth = lineHeight
        self.layer.addSublayer(border)
        self.layer.masksToBounds = true
    }

    override func draw(_ rect: CGRect) {
        border.frame = CGRect(x: 0, y: self.frame.size.height - lineHeight, width:  self.frame.size.width, height: self.frame.size.height)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        border.frame = CGRect(x: 0, y: self.frame.size.height - lineHeight, width:  self.frame.size.width, height: self.frame.size.height)
        self.delegate = self
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        border.borderColor = selectedLineColor.cgColor
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        border.borderColor = lineColor.cgColor
    }
}


从情节提要中设置
lineColor
selectedLineColor
,然后运行项目。

只需使用图像作为背景,图像底部包含一条线,上面是透明的。这种图像将满足您的需求。与其制作整个
,不如制作
扩展名
?如果我有创建类,那么就不需要编写任何代码,只需在情节提要文件中使用,我有创建扩展名,然后我们需要在视图控制器类中使用文本字段编写代码。让我明白这一点。我想创建一个名为UItextField的自定义cocoa类,它是UItextField的子类?该类的名称不是UItextField,而是UItextField的子类。您可以为这个类指定任何名称,如UITextField\u BottomBorder,然后将其设置在情节提要文件中,该文件几乎可以完美地工作。非常感谢!虽然我有一个关于这个的请求,那就是改变颜色,使之成为十六进制值或RGB值。如何将“border.borderColor=UIColor.darkGrayColor().CGColor”更改为具有RGB值或十六进制值的内容?