2021-07-16 15:21:18 +02:00
|
|
|
//
|
2021-07-19 11:12:45 +02:00
|
|
|
// StatusEditorView.swift
|
|
|
|
//
|
2021-07-16 15:21:18 +02:00
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-7-16.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import SwiftUI
|
2021-07-19 11:12:45 +02:00
|
|
|
import UITextView_Placeholder
|
2021-07-16 15:21:18 +02:00
|
|
|
|
2021-07-19 11:12:45 +02:00
|
|
|
public struct StatusEditorView: UIViewRepresentable {
|
2021-07-16 15:21:18 +02:00
|
|
|
|
|
|
|
@Binding var string: String
|
2021-07-19 11:12:45 +02:00
|
|
|
let placeholder: String
|
2021-07-16 15:21:18 +02:00
|
|
|
let width: CGFloat
|
|
|
|
let attributedString: NSAttributedString
|
2021-07-19 11:12:45 +02:00
|
|
|
let keyboardType: UIKeyboardType
|
2021-07-20 10:40:04 +02:00
|
|
|
@Binding var viewDidAppear: Bool
|
2021-07-16 15:21:18 +02:00
|
|
|
|
|
|
|
public init(
|
|
|
|
string: Binding<String>,
|
2021-07-19 11:12:45 +02:00
|
|
|
placeholder: String,
|
2021-07-16 15:21:18 +02:00
|
|
|
width: CGFloat,
|
2021-07-19 11:12:45 +02:00
|
|
|
attributedString: NSAttributedString,
|
2021-07-20 10:40:04 +02:00
|
|
|
keyboardType: UIKeyboardType,
|
|
|
|
viewDidAppear: Binding<Bool>
|
2021-07-16 15:21:18 +02:00
|
|
|
) {
|
|
|
|
self._string = string
|
2021-07-19 11:12:45 +02:00
|
|
|
self.placeholder = placeholder
|
2021-07-16 15:21:18 +02:00
|
|
|
self.width = width
|
|
|
|
self.attributedString = attributedString
|
2021-07-19 11:12:45 +02:00
|
|
|
self.keyboardType = keyboardType
|
2021-07-20 10:40:04 +02:00
|
|
|
self._viewDidAppear = viewDidAppear
|
2021-07-16 15:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public func makeUIView(context: Context) -> UITextView {
|
|
|
|
let textView = UITextView(frame: .zero)
|
2021-07-19 11:12:45 +02:00
|
|
|
textView.placeholder = placeholder
|
2021-07-16 15:21:18 +02:00
|
|
|
|
|
|
|
textView.isScrollEnabled = false
|
|
|
|
textView.font = .preferredFont(forTextStyle: .body)
|
|
|
|
textView.textColor = .label
|
2021-07-19 11:12:45 +02:00
|
|
|
textView.keyboardType = keyboardType
|
2021-07-16 15:21:18 +02:00
|
|
|
textView.delegate = context.coordinator
|
2021-07-20 13:24:24 +02:00
|
|
|
textView.backgroundColor = .clear
|
2021-07-16 15:21:18 +02:00
|
|
|
|
|
|
|
textView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
let widthLayoutConstraint = textView.widthAnchor.constraint(equalToConstant: 100)
|
|
|
|
widthLayoutConstraint.priority = .required - 1
|
|
|
|
context.coordinator.widthLayoutConstraint = widthLayoutConstraint
|
2021-07-20 10:40:04 +02:00
|
|
|
|
2021-07-16 15:21:18 +02:00
|
|
|
return textView
|
|
|
|
}
|
|
|
|
|
|
|
|
public func updateUIView(_ textView: UITextView, context: Context) {
|
2022-04-28 21:41:13 +02:00
|
|
|
// preserve currently selected text range to prevent cursor jump
|
|
|
|
let currentlySelectedRange = textView.selectedRange
|
|
|
|
|
2021-07-16 15:21:18 +02:00
|
|
|
// update content
|
|
|
|
// textView.attributedText = attributedString
|
|
|
|
textView.text = string
|
|
|
|
|
|
|
|
// update layout
|
|
|
|
context.coordinator.updateLayout(width: width)
|
2021-07-20 10:40:04 +02:00
|
|
|
|
|
|
|
// set becomeFirstResponder
|
|
|
|
if viewDidAppear {
|
|
|
|
viewDidAppear = false
|
|
|
|
textView.becomeFirstResponder()
|
|
|
|
}
|
2022-04-28 21:41:13 +02:00
|
|
|
|
|
|
|
// restore selected text range
|
|
|
|
textView.selectedRange = currentlySelectedRange
|
2021-07-16 15:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public func makeCoordinator() -> Coordinator {
|
|
|
|
Coordinator(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Coordinator: NSObject, UITextViewDelegate {
|
2021-07-19 11:12:45 +02:00
|
|
|
var parent: StatusEditorView
|
2021-07-16 15:21:18 +02:00
|
|
|
var widthLayoutConstraint: NSLayoutConstraint?
|
|
|
|
|
2021-07-19 11:12:45 +02:00
|
|
|
init(_ parent: StatusEditorView) {
|
2021-07-16 15:21:18 +02:00
|
|
|
self.parent = parent
|
|
|
|
}
|
|
|
|
|
|
|
|
public func textViewDidChange(_ textView: UITextView) {
|
2022-03-29 11:51:14 +02:00
|
|
|
// prevent break IME input
|
|
|
|
if textView.markedTextRange == nil {
|
|
|
|
parent.string = textView.text
|
|
|
|
}
|
2021-07-16 15:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateLayout(width: CGFloat) {
|
|
|
|
guard let widthLayoutConstraint = widthLayoutConstraint else { return }
|
|
|
|
widthLayoutConstraint.constant = width
|
|
|
|
widthLayoutConstraint.isActive = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|