Swift文本只显示前两行

zdwk9cvp  于 2023-04-28  发布在  Swift
关注(0)|答案(1)|浏览(97)

我正在创建一个聊天机器人,它最终将使用ChatGPT API。现在我正在创建聊天部分。我可以在textfield中输入并添加到数组中,然后使用Text()显示数组,如代码所示。但是,当键入一个长消息时,它只会显示2行,然后。...如图所示。我如何让它使用超过2行并显示它全部。我试过使用lineLimit,但它没有什么区别。

import SwiftUI

struct writtenpromptView: View {
    
    @State private var Prompt: String = ""

    @State var chatarray: [String] = ["Hello", "hello"]
    @State var aiarray: [String] = ["good question", "hello"]
    
    var body: some View {
        ZStack {
            Color("darkgpt")
                .edgesIgnoringSafeArea(.all)
            VStack {
                topbar()
                    .padding(.top, 35)
                    .edgesIgnoringSafeArea(.all)
                    .frame(height:45)
                    .padding(.bottom, 10)
                ScrollView {
                    ScrollViewReader { value in
                        GeometryReader { geometry in
                            VStack {
                                ForEach(0..<chatarray.count, id:\.self) { index in
                                        Rectangle()
                                        .foregroundColor(Color("darkgpt"))
                                        .frame(minWidth: geometry.size.width, maxWidth: geometry.size.width, minHeight: 50, maxHeight: 500)
                                        .overlay(Text(chatarray[index]).foregroundColor(Color("textgrey")).frame(minWidth: 50, maxWidth: geometry.size.width - 60, minHeight: 50, maxHeight: 500))
                                    
                                        Rectangle()
                                        .foregroundColor(Color("gptresponse"))
                                        .frame(minWidth: geometry.size.width, maxWidth: geometry.size.width, minHeight: 50, maxHeight: 500)
                                        .overlay(Text(aiarray[index]).foregroundColor(Color("textgrey")).frame(minWidth: 50, maxWidth: geometry.size.width - 60, minHeight: 50, maxHeight: 500)
                                        )
                                    
                                    
                                }
                            }
                        }
                    }
            }
                
                ZStack {
                    if Prompt == "" {
                        HStack {
                            TypingAnimationView(promptArray: ["Ask us anything...", "Explain quantum computing in simple terms.", "Write an essay on George Washington.", "Tell me a joke.", "write code to print \"hello World\" in python."], typingSpeed: 0.09, deletionSpeed: 0.09)
                        }
                    }
                        
                    TextField("", text: $Prompt)
                        .padding()
                        .frame(width: 300, height: 50)
                        .background(Color("textbar").opacity(0.3).cornerRadius(20))
                        .padding(.bottom, 25)
                        .foregroundColor(Color.white)
                        .font(.headline)
                        .onSubmit {
                            chatarray.append(self.Prompt)
                            aiarray.append(self.Prompt)
                            self.Prompt = ""
                        }
                }
            }
        }
    }
}

struct writtenpromptView_Previews: PreviewProvider {
    static var previews: some View {
        writtenpromptView()
    }
}

bt1cpqcv

bt1cpqcv1#

在找到答案之前我做了很大的修改,但是即使在发布的版本中,你也应该能够通过删除scrollViewReader和/或几何阅读器来修复它。

相关问题