mastodon-ios/ShareActionExtension/Scene/View/StatusEditorView.swift

96 lines
2.6 KiB
Swift
Raw Normal View History

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
@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,
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
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
textView.translatesAutoresizingMaskIntoConstraints = false
let widthLayoutConstraint = textView.widthAnchor.constraint(equalToConstant: 100)
widthLayoutConstraint.priority = .required - 1
context.coordinator.widthLayoutConstraint = widthLayoutConstraint
2021-07-16 15:21:18 +02:00
return textView
}
public func updateUIView(_ textView: UITextView, context: Context) {
// update content
// textView.attributedText = attributedString
textView.text = string
// update layout
context.coordinator.updateLayout(width: width)
// set becomeFirstResponder
if viewDidAppear {
viewDidAppear = false
textView.becomeFirstResponder()
}
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) {
parent.string = textView.text
}
func updateLayout(width: CGFloat) {
guard let widthLayoutConstraint = widthLayoutConstraint else { return }
widthLayoutConstraint.constant = width
widthLayoutConstraint.isActive = true
}
}
}