简体   繁体   English

设置 UIViewRepresentable 高度

[英]Setting UIViewRepresentable height

I have some UITextView which I want to place inside a VStack.我有一些 UITextView 想放在 VStack 中。 I want them to display one directly under the other and completely expanded, without having to scroll inside the TextView.我希望他们直接在另一个下方显示一个并完全展开,而不必在 TextView 内滚动。

struct TextView: UIViewRepresentable {
    let text : String
    let titulo : String
    
    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        
        textView.isEditable = false
        textView.isSelectable = false
        textView.bounces = false
        
        DispatchQueue.main.async {//Needs to execute in another thread, otherwise it crashes (Swift bug)
            textView.attributedText = text.htmlToAttributedString(size: 16, titulo: titulo)
        }
        
        return textView
    }
    
    func updateUIView(_ uiView: UITextView, context: Context) {
    }
}

My view looks like:我的观点如下:

struct PageView: View {

    let sampleText = """
<h1>Lorem ipsum dolor sit amet,</h1>
<h2>consectetur adipiscing elit,</h2>
<h3> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<h3>
<p> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><br>
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.<br>
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
"""
    
    var body: some View{
        TabView{
            HStack{
                VStack{
                    //Views that take up half of the screen
                }
                VStack(alignment: .leading){
                    ScrollView{
                        TextView(text: sampleText, titulo: "")
                        TextView(text: sampleText, titulo: "")
                        Spacer()
                    }
                }
            }
        }.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
    }
}

And my htmlToAttributedString function:还有我的 htmlToAttributedString 函数:

    func htmlToAttributedString(size: CGFloat, titulo: String) -> NSAttributedString? {
        
        var attributedString = NSAttributedString()
        
        let texto = self.replacingOccurrences(of: "\n", with: "<br>")

        
        if(!self.isEmpty)
        {
            var htmlTemplate = """
            <!doctype html>
            <html>
              <head>
                <style>
                  body {
                    font-family: -apple-system;
                    font-size: \(size)px;
                  }
                </style>
              </head>
              <body>
            """
            
            if(titulo != "")
            {
                htmlTemplate += "<h3>\(titulo)</h3>"
            }
            
            htmlTemplate += """
                \(texto)
              </body>
            </html>
            """

                guard let data = htmlTemplate.data(using: .utf8) else { return NSAttributedString(string: "No se han obtenido los datos correctamente")}
                    do {

                        attributedString = try NSAttributedString(
                              data: data,
                              options: [
                                .documentType: NSAttributedString.DocumentType.html,
                                .characterEncoding: String.Encoding.utf8.rawValue,
                                
                              ],
                              documentAttributes: nil)
                        
                    } catch {}
                  }
            
            return attributedString
        }
}

I've already tried scaledToFit, maxHeight = .infinity and fixedSize.我已经尝试过 scaledToFit、maxHeight = .infinity 和 fixedSize。

Well, the view doesn't display just because the ScrollView , eliminate the ScrollView to the following code:好吧,视图不显示只是因为ScrollView ,将ScrollView消除为以下代码:

    var body: some View{
        TabView{
            HStack{
                VStack{
                    //Views that take up half of the screen
                    Image(systemName: "plus")
                }
                TextView(text: sampleText, titulo: "")
                TextView(text: sampleText, titulo: "")
            }
        }
        .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))

Maybe this will be the result that you want.也许这将是您想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM