-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0326b0
commit 7d3176f
Showing
3 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// TextView.swift | ||
// AirPushMac | ||
// | ||
// Created by Alexander Ignatev on 08.07.2020. | ||
// Copyright © 2020 Alexandr Ignatyev. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct TextView: NSViewRepresentable { | ||
@Binding var text: String | ||
|
||
func makeNSView(context: Context) -> NSScrollView { | ||
let textView = NSTextView() | ||
textView.delegate = context.coordinator | ||
textView.isRichText = false | ||
textView.autoresizingMask = [.width] | ||
textView.translatesAutoresizingMaskIntoConstraints = true | ||
textView.isVerticallyResizable = true | ||
textView.isHorizontallyResizable = false | ||
textView.isEditable = true | ||
textView.font = NSFont.monospacedSystemFont( | ||
ofSize: NSFont.systemFontSize, | ||
weight: .regular | ||
) | ||
let scrollView = NSScrollView() | ||
scrollView.documentView = textView | ||
scrollView.hasVerticalScroller = true | ||
scrollView.borderType = .bezelBorder | ||
return scrollView | ||
} | ||
|
||
func updateNSView(_ scrollView: NSScrollView, context: Context) { | ||
(scrollView.documentView as? NSTextView)?.string = text | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator(self) | ||
} | ||
|
||
final class Coordinator: NSObject, NSTextViewDelegate { | ||
let parent: TextView | ||
|
||
init(_ parent: TextView) { | ||
self.parent = parent | ||
} | ||
|
||
func textDidChange(_ notification: Notification) { | ||
guard let textView = notification.object as? NSTextView else { return } | ||
parent.text = textView.string | ||
} | ||
} | ||
} | ||
|
||
struct TextView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
TextView(text: .constant("ABCabc123")) | ||
.frame(width: 400, height: 300) | ||
.padding() | ||
} | ||
} |