Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可在SwiftUI中表示的UITextView不考虑ScrollView中的宽度边界_Swift_Xcode_Swiftui_Uitextview - Fatal编程技术网

可在SwiftUI中表示的UITextView不考虑ScrollView中的宽度边界

可在SwiftUI中表示的UITextView不考虑ScrollView中的宽度边界,swift,xcode,swiftui,uitextview,Swift,Xcode,Swiftui,Uitextview,我想使用SwiftUI在UITextView中显示文本。我使用的是UIViewRepresentable,当宽度超出scrollView的边界时,它会出现问题。我想包装文本,但它只是在一条长线上继续,左右不可见: 这是我的代码: struct ContentView: View { var messages = ["here is some long text to show the textView is not wrapping it's content&quo

我想使用SwiftUI在UITextView中显示文本。我使用的是UIViewRepresentable,当宽度超出scrollView的边界时,它会出现问题。我想包装文本,但它只是在一条长线上继续,左右不可见:

这是我的代码:

struct ContentView: View {
    
    var messages = ["here is some long text to show the textView is not wrapping it's content"]
    
    var body: some View {
        GeometryReader { geometry in
            VStack(spacing: 0) {
                ScrollView(.vertical) {
                    ScrollViewReader { scrollView in
                        ZStack {
                            LazyVStack {
                                ForEach(messages, id: \.self) { message in
                                    TextView(text: message)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

struct TextView: UIViewRepresentable {
    
    var text: String
     
    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
                
        textView.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body)
        textView.isScrollEnabled = false
        textView.backgroundColor = .clear
        textView.isEditable = false
        
        textView.clipsToBounds = true
        
        textView.isEditable = false
        
        textView.textContainerInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
        textView.backgroundColor = #colorLiteral(red: 0.03137254902, green: 0.4980392157, blue: 0.9960784314, alpha: 1)
        textView.textColor = UIColor.white

        textView.text = text

        return textView
    }
 
    func updateUIView(_ uiView: UITextView, context: Context) {
        
    }
}

我想知道这是否与scrollView有关。不管怎样,我想知道是否有解决办法?我尝试将帧宽度设置为geometry.size.width,但没有效果。

发生这种情况是因为您使用的是UIRepresentable视图。有几种解决方案,其中最简单的是在SwiftUI视图中的UIRepresentable视图顶部使用显式
.frame()


e、 g.
.frame(width:UIScreen.main.bounds.width)
所以它只能扩展到屏幕的宽度。

@Asperi我看到了你的UILabel解决方案,但它在这里不起作用。这不起作用。这不是一个很好的解决方案,因为我可能想在堆栈中添加其他元素,所以我不想局限于始终使用屏幕宽度。我想让它知道它的宽度和包装文本的无限高度的界限,因为它是在一个滚动视图嗯,首先,你是对的,这不适用于你的情况,我道歉。它仍然适用于大多数其他UIRepresentable。其次,如果您使用的是UIRepresentable,那么不幸的是,您失去了SwiftUI的部分优秀功能,无法自动调整视图的大小。所以说实话,我明白你的意思,但我认为这是不可能的。我可能错了。