程序员

Swift TextField添加自定义下划线

2020-08-12  本文已影响0人  晴朗Nic

1、新建一个文件继承自UITextField文件

2、在draw的方法里面绘出一条下划线,线条的高度和颜色都可以自己定义,如下所示:

class CurrencyUnderLineTextField: UITextField {
    
    override func draw(_ rect: CGRect) {
        //线条的高度
        let lineHeight : CGFloat = 0.5
        //线条的颜色
        let lineColor = UIColor.gray
        
        guard let content = UIGraphicsGetCurrentContext() else { return }
        content.setFillColor(lineColor.cgColor)
        content.fill(CGRect.init(x: 0, y: self.frame.height - lineHeight, width: self.frame.width, height: lineHeight))
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
    }
}

3、使用的时候就这如下:

let lineTextField = CurrencyUnderLineTextField()
上一篇 下一篇

猜你喜欢

热点阅读