PDPS
PDPS

Reputation: 31

Limit height of HTML-rendering view

I have to display several HTML-Content-Strings using SwiftUI. For rendering the HTML-Content I "borrowed" one of the standard solutions that wraps a WKWebCiew() in an UIViewRepresentable context. Unfortunately, the view consumes to much vertical space. The view is just to large.

  1. Is there a way to shrink the view automatically to minimal height?
  2. Is there a way to change the font of webview?
import SwiftUI
 import WebKit
 
struct HTMLText: UIViewRepresentable {
    let htmlContent: String
    
    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        uiView.loadHTMLString(htmlContent, baseURL: nil)
    }
}

let htmlContent1 = """
<ul>
   <li> Item 1
   <li> Item 2
   <li> Item 3
   <li> Item 4
</ul>
"""

struct ContentView: View {
    var body : some View {
        VStack (alignment: .leading){
            Text("Above")
            HTMLText(htmlContent: htmlContent1)
            Text("Middle")
            HTMLText(htmlContent: htmlContent1)
            Text("Below")
        }.padding()
    }
}

 #Preview {
 ContentView()
 }

Upvotes: 0

Views: 44

Answers (0)

Related Questions