Skip to content

Instantly share code, notes, and snippets.

@Sangsom
Last active April 17, 2020 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sangsom/281248066a21f9ce8173d78a5d49366f to your computer and use it in GitHub Desktop.
Save Sangsom/281248066a21f9ce8173d78a5d49366f to your computer and use it in GitHub Desktop.
This is a custom multiline SwiftUI TextView integrated from UITextView
import SwiftUI
import UIKit
struct NotesTextView: UIViewRepresentable {
@Binding var text: String
class Coordinator: NSObject, UITextViewDelegate {
@Binding var text: String
init(text: Binding<String>) {
_text = text
}
func textViewDidChangeSelection(_ textView: UITextView) {
text = textView.text ?? ""
}
}
func makeUIView(context: Context) -> UITextView {
let textView = UITextView(frame: .zero)
textView.delegate = context.coordinator
textView.font = UIFont.systemFont(ofSize: 17, weight: .regular)
textView.textContainerInset = UIEdgeInsets(top: 8, left: -5, bottom: 8, right: 0)
return textView
}
func makeCoordinator() -> Coordinator {
return Coordinator(text: $text)
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment