Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ViewController: UIViewController {

// UITextField traditional properties
self.emailTextField.placeholder = "Email"
self.emailTextField.info = "Enter your email here"
self.emailTextField.enablesReturnKeyAutomatically = true
self.emailTextField.returnKeyType = .next
self.emailTextField.clearButtonMode = .whileEditing
Expand Down
Binary file modified README/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 45 additions & 1 deletion Sources/TKFormTextField/TKFormTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ open class TKFormTextField: UITextField {
fileprivate final func setup() {
self.borderStyle = .none
self.createTitleLabel()
self.createInfoLabel()
self.createErrorLabel()
self.createLineView()
self.updateColors()
Expand All @@ -43,6 +44,7 @@ open class TKFormTextField: UITextField {
// MARK: Views
open var titleLabel: UILabel! // the label above input text
open var lineView: UIView! // the line below input text
open var infoLabel: UILabel! // the info label below input text
open var errorLabel: UILabel! // the error label below input text

// MARK: Animation
Expand Down Expand Up @@ -92,6 +94,12 @@ open class TKFormTextField: UITextField {
}
}

open var infoColor: UIColor = UIColor.lightGray {
didSet {
self.updateColors()
}
}

open var errorColor: UIColor = UIColor.red {
didSet {
self.updateColors()
Expand Down Expand Up @@ -125,6 +133,13 @@ open class TKFormTextField: UITextField {
}
}

// A string for infoLabel
open var info: String? {
didSet {
self.updateControl(true)
}
}

// A string for errorLabel
open var error: String? {
didSet {
Expand Down Expand Up @@ -210,7 +225,19 @@ open class TKFormTextField: UITextField {
view.accessibilityIdentifier = "line-view"
self.addSubview(view)
}


fileprivate func createInfoLabel() {
let label = UILabel()
label.autoresizingMask = [.flexibleWidth, .flexibleHeight]
label.font = UIFont.systemFont(ofSize: 13)
label.alpha = 1.0
label.numberOfLines = 0
label.textColor = self.infoColor
label.accessibilityIdentifier = "info-label"
self.addSubview(label)
self.infoLabel = label
}

fileprivate func createErrorLabel() {
let label = UILabel()
label.autoresizingMask = [.flexibleWidth, .flexibleHeight]
Expand Down Expand Up @@ -248,6 +275,7 @@ open class TKFormTextField: UITextField {
self.updateColors()
self.updateLineView()
self.updateTitleLabel(animated)
self.updateInfoLabel()
self.updateErrorLabel(animated)
}

Expand Down Expand Up @@ -295,6 +323,11 @@ open class TKFormTextField: UITextField {
self.titleLabel.text = self.titleOrPlaceholder()
self.updateTitleVisibility(animated)
}

fileprivate func updateInfoLabel(_ animated:Bool = false) {
self.infoLabel.text = info
self.invalidateIntrinsicContentSize()
}

fileprivate func updateErrorLabel(_ animated:Bool = false) {
self.errorLabel.text = error
Expand Down Expand Up @@ -383,6 +416,16 @@ open class TKFormTextField: UITextField {
let lineHeight: CGFloat = editing ? CGFloat(self.selectedLineHeight) : CGFloat(self.lineHeight)
return CGRect(x: 0, y: bounds.size.height - lineHeight - errorHeight(), width: bounds.size.width, height: lineHeight)
}

open func infoLabelRectForBounds(_ bounds: CGRect) -> CGRect {
guard let info = info, !info.isEmpty else { return CGRect.zero }
let font: UIFont = infoLabel.font ?? UIFont.systemFont(ofSize: 17.0)

let textAttributes = [NSAttributedString.Key.font: font]
let s = CGSize(width: bounds.size.width, height: 2000)
let boundingRect = info.boundingRect(with: s, options: .usesLineFragmentOrigin, attributes: textAttributes, context: nil)
return CGRect(x: 0, y: bounds.size.height, width: boundingRect.size.width, height: boundingRect.size.height)
}

open func errorLabelRectForBounds(_ bounds: CGRect) -> CGRect {
guard let error = error, !error.isEmpty else { return CGRect.zero }
Expand Down Expand Up @@ -430,6 +473,7 @@ open class TKFormTextField: UITextField {
self.invalidateIntrinsicContentSize()
self.titleLabel.frame = self.titleLabelRectForBounds(self.bounds, editing: self.isTitleVisible() || _renderingInInterfaceBuilder)
self.errorLabel.frame = self.errorLabelRectForBounds(self.bounds)
self.infoLabel.frame = self.errorLabel.frame == CGRect.zero ? self.infoLabelRectForBounds(self.bounds) : CGRect.zero
self.lineView.frame = self.lineViewRectForBounds(self.bounds, editing: self.editingOrSelected || _renderingInInterfaceBuilder)

// Fix unwanted rightView sliding in animation when it's first shown
Expand Down
1 change: 1 addition & 0 deletions TKFormTextFieldDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ViewController: UIViewController {
self.emailTextField.enablesReturnKeyAutomatically = true
self.emailTextField.returnKeyType = .next
self.emailTextField.clearButtonMode = .whileEditing
self.emailTextField.info = "Enter your email here"
self.emailTextField.delegate = self

self.passwordTextField.placeholder = "Password"
Expand Down