GBA-8-19/Delta/Pause Menu/Cheats/gp_textV.swift
2024-06-14 17:15:51 +08:00

66 lines
2.3 KiB
Swift

//
// gp_textV.swift
// Hthik
//
// Created by 16 on 2024/5/31.
// Copyright © 2024 Hthik. All rights reserved.
//
import Foundation
import UIKit
class TextViewModifier {
// TextView
static func setText(of textView: UITextView, to text: String) {
textView.text = text
}
// TextView
static func setTextColor(of textView: UITextView, to color: UIColor) {
textView.textColor = color
}
// TextView
static func setBackgroundColor(of textView: UITextView, to color: UIColor) {
textView.backgroundColor = color
}
// TextView
static func setFont(of textView: UITextView, to font: UIFont) {
textView.font = font
}
// TextView
static func setPlaceholder(of textView: UITextView, to placeholder: String, placeholderColor: UIColor = .lightGray) {
textView.text = placeholder
textView.textColor = placeholderColor
textView.delegate = TextViewPlaceholderDelegate.shared
TextViewPlaceholderDelegate.shared.setPlaceholder(textView: textView, placeholder: placeholder, placeholderColor: placeholderColor)
}
}
//
class TextViewPlaceholderDelegate: NSObject, UITextViewDelegate {
static let shared = TextViewPlaceholderDelegate()
private var placeholderMap = [UITextView: (placeholder: String, placeholderColor: UIColor, originalColor: UIColor)]()
func setPlaceholder(textView: UITextView, placeholder: String, placeholderColor: UIColor) {
let originalColor = textView.textColor ?? .black
placeholderMap[textView] = (placeholder, placeholderColor, originalColor)
}
func textViewDidBeginEditing(_ textView: UITextView) {
guard let placeholderData = placeholderMap[textView], textView.text == placeholderData.placeholder else { return }
textView.text = nil
textView.textColor = placeholderData.originalColor
}
func textViewDidEndEditing(_ textView: UITextView) {
guard let placeholderData = placeholderMap[textView], textView.text.isEmpty else { return }
textView.text = placeholderData.placeholder
textView.textColor = placeholderData.placeholderColor
}
}