首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置UIViewRepresentable高度

设置UIViewRepresentable高度
EN

Stack Overflow用户
提问于 2022-07-05 14:42:50
回答 1查看 90关注 0票数 0

我有一些UITextView,我想把它放在VStack里面。我希望它们直接显示在另一个下面,并且完全扩展,而不必在TextView中滚动。

代码语言:javascript
复制
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) {
    }
}

我的看法是:

代码语言:javascript
复制
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))
    }
}

我的htmlToAttributedString函数:

代码语言:javascript
复制
    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
        }
}

我已经试过了scaledToFit,maxHeight = .infinity和fixedSize。

EN

回答 1

Stack Overflow用户

发布于 2022-07-06 09:31:04

好吧,视图的显示并不仅仅是因为ScrollView,消除了ScrollView到下面的代码:

代码语言:javascript
复制
    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))

也许这就是你想要的结果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72871325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档