Bark/View/BorderTextField.swift
2024-10-25 15:30:01 +08:00

103 lines
3.1 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// BorderTextField.swift
// Bark
//
// Created by huangfeng on 2023/2/6.
// Copyright © 2023 Fin. All rights reserved.
//
import UIKit
class InsetTextField: UITextField {
var insets = UIEdgeInsets.zero
override func textRect(forBounds bounds: CGRect) -> CGRect {
let bounds = super.textRect(forBounds: bounds)
return bounds.inset(by: insets)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
let bounds = super.textRect(forBounds: bounds)
return bounds.inset(by: insets)
}
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
let bounds = super.textRect(forBounds: bounds)
return bounds.inset(by: insets)
}
}
class BorderTextField: InsetTextField {
var isSelecting: Bool = true {
didSet {
UIView.animate(withDuration: 0.3) {
if self.isSelecting {
self.backgroundView.borderColor = BKColor.blue.darken5
self.backgroundView.shadowColor = BKColor.blue.darken5
self.backgroundView.layer.shadowOpacity = 0.3
}
else {
self.backgroundView.borderColor = BKColor.grey.lighten2
self.backgroundView.shadowColor = BKColor.grey.lighten2
self.backgroundView.layer.shadowOpacity = 0
}
}
}
}
let backgroundView: UIView = {
let view = UIView()
view.backgroundColor = BKColor.white
view.isUserInteractionEnabled = false
view.cornerRadiusPreset = .cornerRadius3
view.shadowColor = BKColor.grey.lighten2
view.layer.shadowOffset = CGSize(width: 0, height: 0)
view.layer.shadowRadius = 2
view.layer.shadowOpacity = 0
view.borderColor = BKColor.grey.lighten2
view.borderWidthPreset = .border2
return view
}()
init(title: String? = nil) {
super.init(frame: CGRect.zero)
self.insets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
self.textColor = BKColor.grey.darken3
self.font = UIFont.preferredFont(ofSize: 14)
self.adjustsFontForContentSizeCategory = true
self.textAlignment = .left
self.insertSubview(backgroundView, at: 0)
backgroundView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
self.delegate = self
}
override var placeholder: String? {
didSet{
self.attributedPlaceholder = NSAttributedString(string: placeholder ?? "" , attributes: [
.font: self.font ?? UIFont.preferredFont(ofSize: 14),
.foregroundColor: BKColor.grey.darken1
])
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension BorderTextField: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
self.isSelecting = true
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.isSelecting = false
}
}