如何在Mac OS的SwiftUI中创建多行文本字段? [英] How do I create a Multi Line Text Field in SwiftUI for MacOS?
问题描述
我正在使用Swift编写我的第一个MacOS应用程序.我需要创建一个允许多个段落的简单可编辑文本框.在HTML中,这将是 textarea
.
我了解到iOS 14将包含 TextEditor
,但这不是现在,而且我也不知道MacOS中是否会包含该代码.
我见过许多解决方案都假定使用iOS和UIKit.
我该如何使用SwiftUI和MacOS?
更新
有人建议这个问题与此类似:
struct TestTextArea:视图{@State private var text =占位符:输入一些文本"var body:some View {VStack {TextArea(文本:$ text).border(彩色.黑色)//文本(文本)//取消注释以查看输入的文本的镜像}.填充()}}struct TextArea:NSViewRepresentable {@Binding var text:字符串func makeNSView(context:Context)->NSScrollView {context.coordinator.createTextViewStack()}func updateNSView(_ nsView:NSScrollView,context:Context){如果让textArea = nsView.documentView为?NSTextView,textArea.string!= self.text {textArea.string = self.text}}func makeCoordinator()->协调员{协调人(文本:$文本)}类协调器:NSObject,NSTextViewDelegate {var文字:Binding< String>init(text:Binding< String>){self.text = 文本}func textView(_ textView:NSTextView,shouldChangeTextIn范围:NSRange,replaceString text:String?)->布尔{推迟 {self.text.wrappedValue =(textView.string as NSString).replacingCharacters(in:range,with:text!)}返回真}fileprivate惰性var textStorage = NSTextStorage()fileprivate惰性var layoutManager = NSLayoutManager()fileprivate惰性var textContainer = NSTextContainer()fileprivate惰性var textView:NSTextView = NSTextView(frame:CGRect(),textContainer:textContainer)fileprivate惰性var scrollview = NSScrollView()func createTextViewStack()->NSScrollView {让contentSize = scrollview.contentSizetextContainer.containerSize = CGSize(width:contentSize.width,height:CGFloat.greatestFiniteMagnitude)textContainer.widthTracksTextView = truetextView.minSize = CGSize(宽度:0,高度:0)textView.maxSize = CGSize(宽度:CGFloat.greatestFiniteMagnitude,高度:CGFloat.greatestFiniteMagnitude)textView.isVerticallyResizable = truetextView.frame = CGRect(x:0,y:0,宽度:contentSize.width,高度:contentSize.height)textView.autoresizingMask = [.width]textView.delegate =自我scrollview.borderType = .noBorderscrollview.hasVerticalScroller = truescrollview.documentView = textViewtextStorage.addLayoutManager(layoutManager)layoutManager.addTextContainer(textContainer)返回scrollview}}}
I am writing my first MacOS app using Swift. I need to create a simple editable text box which allows multiple paragraphs. In HTML, this would be a textarea
.
I gather that iOS 14 will include TextEditor
, but that’s not now, and I don’t know whether that will be in MacOS anyway.
Many solutions I have seen presume iOS and UIKit.
How do I do this with SwiftUI and MacOS?
Update
Someone has suggested that the question is similar to this one: How do I create a multiline TextField in SwiftUI?
I have already looked at that question, but:
- The linked question is specifically for iOS, but mine is for MacOS
- The answers, as far as I can tell, are specifically for iOS using UIKit. If someone cares to explain how this answers my MacOS question I would be very interested …
Here is some initial demo of component like iOS14 TextEditor.
Demo prepared & tested with Xcode 11.7 / macOS 10.15.6
struct TestTextArea: View {
@State private var text = "Placeholder: Enter some text"
var body: some View {
VStack {
TextArea(text: $text)
.border(Color.black)
// Text(text) // uncomment to see mirror of enterred text
}.padding()
}
}
struct TextArea: NSViewRepresentable {
@Binding var text: String
func makeNSView(context: Context) -> NSScrollView {
context.coordinator.createTextViewStack()
}
func updateNSView(_ nsView: NSScrollView, context: Context) {
if let textArea = nsView.documentView as? NSTextView, textArea.string != self.text {
textArea.string = self.text
}
}
func makeCoordinator() -> Coordinator {
Coordinator(text: $text)
}
class Coordinator: NSObject, NSTextViewDelegate {
var text: Binding<String>
init(text: Binding<String>) {
self.text = text
}
func textView(_ textView: NSTextView, shouldChangeTextIn range: NSRange, replacementString text: String?) -> Bool {
defer {
self.text.wrappedValue = (textView.string as NSString).replacingCharacters(in: range, with: text!)
}
return true
}
fileprivate lazy var textStorage = NSTextStorage()
fileprivate lazy var layoutManager = NSLayoutManager()
fileprivate lazy var textContainer = NSTextContainer()
fileprivate lazy var textView: NSTextView = NSTextView(frame: CGRect(), textContainer: textContainer)
fileprivate lazy var scrollview = NSScrollView()
func createTextViewStack() -> NSScrollView {
let contentSize = scrollview.contentSize
textContainer.containerSize = CGSize(width: contentSize.width, height: CGFloat.greatestFiniteMagnitude)
textContainer.widthTracksTextView = true
textView.minSize = CGSize(width: 0, height: 0)
textView.maxSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
textView.isVerticallyResizable = true
textView.frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height)
textView.autoresizingMask = [.width]
textView.delegate = self
scrollview.borderType = .noBorder
scrollview.hasVerticalScroller = true
scrollview.documentView = textView
textStorage.addLayoutManager(layoutManager)
layoutManager.addTextContainer(textContainer)
return scrollview
}
}
}
这篇关于如何在Mac OS的SwiftUI中创建多行文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!