119 lines
4.4 KiB
Swift
119 lines
4.4 KiB
Swift
//
|
|
// RoundedTextField.swift
|
|
// Privado
|
|
//
|
|
// Created by Juraldinio on 2/17/20.
|
|
// Copyright © 2020 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
final class RoundedTextField: UITextField {
|
|
|
|
// MARK: - Customize
|
|
struct Customize {
|
|
let textColor: UIColor
|
|
let backgroundColor: UIColor
|
|
let cornerRadius: CGFloat
|
|
let errorBorderHeight: CGFloat
|
|
let errorBorderColor: UIColor
|
|
let autoCapitalization: UITextAutocapitalizationType
|
|
let attrPlaceholder: [NSAttributedString.Key: Any]
|
|
let borderColor = UIColor(red: 45, green: 51, blue: 82)
|
|
let activeBorderColor = UIColor(red: 42, green: 216, blue: 153)
|
|
}
|
|
|
|
private static let LoginAttrPlaceholder = [NSAttributedString.Key.foregroundColor: UIColor(red: 173, green: 179, blue: 210), NSAttributedString.Key.font: UIFont(name: "SFProText-Regular", size: 21)]
|
|
|
|
static let LoginCustomize = Customize(textColor: UIColor(red: 173, green: 179, blue: 210),
|
|
backgroundColor: UIColor(rgb: 0x2D3352),
|
|
cornerRadius: 24,
|
|
errorBorderHeight: 3,
|
|
errorBorderColor: UIColor(red: 228, green: 59, blue: 105),
|
|
autoCapitalization: .none,
|
|
attrPlaceholder: RoundedTextField.LoginAttrPlaceholder)
|
|
|
|
private var bottomLine = UIView()
|
|
|
|
private var borderWidth: CGFloat?
|
|
private var borderColor: UIColor?
|
|
private var activeBorderColor: UIColor?
|
|
private var borderErrorColor: UIColor?
|
|
|
|
// MARK: - Init
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
|
|
self.setup()
|
|
}
|
|
|
|
convenience init() {
|
|
self.init(frame: .zero)
|
|
|
|
self.setup()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
// MARK: -
|
|
|
|
private func setup() {
|
|
self.translatesAutoresizingMaskIntoConstraints = false
|
|
}
|
|
|
|
func customize(with customize: Customize, placeholder: String) {
|
|
|
|
self.textColor = customize.textColor
|
|
self.font = UIFont(name: "SFProText-Regular", size: 21)
|
|
|
|
self.bottomLine.translatesAutoresizingMaskIntoConstraints = false
|
|
self.addSubview(self.bottomLine)
|
|
self.bottomLine.backgroundColor = customize.borderColor
|
|
NSLayoutConstraint.activate([
|
|
self.bottomLine.trailingAnchor.constraint(equalTo: self.trailingAnchor),
|
|
self.bottomLine.leadingAnchor.constraint(equalTo: self.leadingAnchor),
|
|
self.bottomLine.heightAnchor.constraint(equalToConstant: customize.errorBorderHeight),
|
|
self.bottomLine.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -customize.errorBorderHeight)
|
|
])
|
|
|
|
|
|
self.autocapitalizationType = customize.autoCapitalization
|
|
|
|
self.borderWidth = customize.errorBorderHeight
|
|
self.borderErrorColor = customize.errorBorderColor
|
|
self.borderColor = customize.borderColor
|
|
self.activeBorderColor = customize.activeBorderColor
|
|
|
|
self.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: customize.attrPlaceholder)
|
|
|
|
self.addTarget(self, action: #selector(textFieldStateChange(object:)), for: .editingDidBegin)
|
|
self.addTarget(self, action: #selector(textFieldStateChange(object:)), for: .editingDidEnd)
|
|
}
|
|
|
|
@objc
|
|
func textFieldStateChange(object: UITextField) {
|
|
guard let activeBorderColor = self.activeBorderColor, let borderColor = self.borderColor else {
|
|
return
|
|
}
|
|
self.bottomLine.backgroundColor = self.isEditing ? activeBorderColor : borderColor
|
|
}
|
|
|
|
override func textRect(forBounds bounds: CGRect) -> CGRect {
|
|
let rect = super.textRect(forBounds: bounds)
|
|
return rect.insetBy(dx: 0, dy: 0)
|
|
}
|
|
|
|
override func editingRect(forBounds bounds: CGRect) -> CGRect {
|
|
let rect = super.editingRect(forBounds: bounds)
|
|
return rect.insetBy(dx: 0, dy: 0)
|
|
}
|
|
|
|
func highlight(isHighlighted: Bool) {
|
|
self.bottomLine.backgroundColor = isHighlighted ? self.borderErrorColor : self.borderColor
|
|
}
|
|
}
|