我有一些UITextView,我想把它放在VStack里面。我希望它们直接显示在另一个下面,并且完全扩展,而不必在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) {
}
}我的看法是:
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函数:
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。
发布于 2022-07-06 09:31:04
好吧,视图的显示并不仅仅是因为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))也许这就是你想要的结果。
https://stackoverflow.com/questions/72871325
复制相似问题