jsdw_ios/QuickLocation/Section/TextInput/TextInputViewController.swift

207 lines
6.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// TextInputViewController.swift
// QuickLocation
//
// Created by on 2026/6/9.
//
import UIKit
import RxSwift
import RxCocoa
///
///
/// let vc = TextInputViewController(title: "", maxLength: 20) { text in
/// print(": \(text)")
/// }
/// present(vc, animated: true)
final class TextInputViewController: UIViewController {
private let titleText: String
private let maxLength: Int
private let confirmAction: ((String) -> Void)?
private let disposeBag = DisposeBag()
private let textRelay = BehaviorRelay<String>(value: "")
// MARK: - Init
/// - Parameters:
/// - title:
/// - maxLength: 0
/// - initialText:
/// - confirmAction:
init(title: String,
maxLength: Int = 0,
initialText: String = "",
confirmAction: ((String) -> Void)? = nil) {
self.titleText = title
self.maxLength = maxLength
self.confirmAction = confirmAction
self.textRelay.accept(initialText)
super.init(nibName: nil, bundle: nil)
modalPresentationStyle = .fullScreen
modalTransitionStyle = .coverVertical
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setupUI()
setupBinding()
textView.becomeFirstResponder()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
// MARK: - UI
private func setupUI() {
view.addSubview(navBgView)
view.addSubview(navView)
view.addSubview(inputTextView)
inputTextView.addSubview(textView)
view.addSubview(countLabel)
view.addSubview(confirmBtn)
navBgView.layoutChain
.edges(excludingEdge: .bottom)
.heightToWidth(160/375)
navView.layoutChain
.edges(excludingEdge: .bottom)
.height(kNaviHeight)
inputTextView.layoutChain
.topToBottomOfView(navView, offset: 15)
.edgesHorzontal(15)
//
textView.layoutChain
.edgesVertical(5)
.edgesHorzontal(10)
countLabel.layoutChain
.topToBottomOfView(inputTextView, offset: 5)
.rightToView(textView)
confirmBtn.layoutChain
.topToBottomOfView(inputTextView, offset: 50)
.edgesHorzontal(15).height(50)
}
// MARK: - Binding
private func setupBinding() {
Observable.merge(
textView.rx.didChange.asObservable(),
textView.rx.text.map { _ in () },
textView.rx.methodInvoked(#selector(UITextView.paste(_:))).map { _ in () }
)
.throttle(.milliseconds(100), scheduler: MainScheduler.instance)
.subscribe(onNext: { [weak self] in
guard let self = self else { return }
if self.textView.text.last == "\n" {
self.textView.text = String(self.textView.text.dropLast())
self.textView.resignFirstResponder()
return
}
let count = self.textView.text.count
if count > self.maxLength {
self.textView.text = String(self.textView.text.prefix(self.maxLength))
self.textView.selectedRange = NSRange(location: self.maxLength, length: 0)
return
}
self.countLabel.text = "\(count)/\(self.maxLength)"
})
.disposed(by: disposeBag)
textRelay.asObservable()
.bind(to: textView.rx.text)
.disposed(by: disposeBag)
//
textView.rx.text.orEmpty.map { text in
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
return !trimmed.isEmpty
}
.bind(to: confirmBtn.rx.isEnabled)
.disposed(by: disposeBag)
confirmBtn.rx.tap.subscribe(onNext: { [weak self] in
guard let self = self, let text = self.textView.text else { return }
self.confirmAction?(text)
self.dismiss(animated: true)
})
.disposed(by: disposeBag)
}
// MARK: - Views
lazy var navBgView: UIImageView = {
let iv = UIImageView()
iv.image = UIImage(named: "Common/navBar_bg_2")
iv.contentMode = .scaleAspectFill
return iv
}()
lazy var navView: BaseNavigationView = {
let nav = BaseNavigationView(title: titleText)
nav.onBackTapped = { [weak self] in
self?.dismiss(animated: true)
}
return nav
}()
lazy var inputTextView: UIView = {
let view = UIView()
view.backgroundColor = .white
view.cornerRadius = 4
view.borderWidth = 0.5
view.borderColor = ThemeManager.shared.color.lineColor
return view
}()
lazy var textView: UITextView = {
let tv = UITextView()
tv.font = .systemFont(ofSize: 15)
tv.textColor = ThemeManager.shared.color.titleAuxColor
tv.backgroundColor = .clear
tv.showsVerticalScrollIndicator = false
tv.isScrollEnabled = false
tv.bounces = false
tv.returnKeyType = .done
return tv
}()
private lazy var countLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 13)
label.textColor = UIColor(hexStr: "#999999")
label.text = maxLength > 0 ? "0/\(maxLength)" : ""
return label
}()
private lazy var confirmBtn: UIButton = {
let btn = UIButton(type: .custom)
btn.setTitle("确定", for: .normal)
btn.setTitleColor(UIColor(hexStr: "#0F2846"), for: .normal)
btn.setBackgroundImage(UIImage(named: "Common/gradient_bg"), for: .normal)
btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
btn.cornerRadius = 25
btn.isEnabled = false
return btn
}()
}