Skip to content

Instantly share code, notes, and snippets.

@kmkrn
Created February 12, 2019 19:33
Show Gist options
  • Save kmkrn/946a856130f8dd8a64dc35b42ff72bda to your computer and use it in GitHub Desktop.
Save kmkrn/946a856130f8dd8a64dc35b42ff72bda to your computer and use it in GitHub Desktop.
A single-bordered UITextField
import UIKit
@IBDesignable class SingleBorderedTextField: UITextField {
private var bottomBorder = UIView()
private (set) var lineStrength: CGFloat = 1
@IBInspectable var lineWidth: CGFloat {
get {
return self.lineStrength
}
set {
self.lineStrength = newValue
}
}
@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
override func awakeFromNib() {
self.translatesAutoresizingMaskIntoConstraints = false
self.borderStyle = .none
bottomBorder = UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
bottomBorder.backgroundColor = self.borderColor
bottomBorder.translatesAutoresizingMaskIntoConstraints = false
addSubview(bottomBorder)
bottomBorder.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
bottomBorder.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
bottomBorder.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
bottomBorder.heightAnchor.constraint(equalToConstant: self.lineStrength).isActive = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment