How to deal with swiftui extra long text displayed as blank?

132 Views Asked by At

About 100,000 text, can be displayed using TextField However, there are other problems that do not want to be used, using Text is just to display a blank, but the position is occupied ScrollView can scroll. Is there a big god to help solve it, the code is very simple, there is nothing special?

VStack(alignment: .leading,spacing: 3){
            ScrollView{
                if let cnt = current?.content{
                    Text(cnt)
                }
            }
        }

i try add .lineLimit(nil),.fixedSize(horizontal: false, vertical: true) None of them worked

I used another way, reading markdown files. The result is still the same

import SwiftUI

struct ContentView: View {
       var body: some View {
        VStack(alignment: .leading,spacing: 3){
            markdownFileView()
        }
    }
}

struct markdownFileView: View {
    let filepath = Bundle.main.url(forResource: "test", withExtension: "md")
    var body: some View {
        ScrollView{
            LazyVStack{
                // Show the markdown here
                Text(try! AttributedString(
                    contentsOf: filepath!,
                    options: AttributedString.MarkdownParsingOptions(
                        interpretedSyntax: .inlineOnlyPreservingWhitespace
                    )))
            }
        }
    }
}

@main
struct CustomPickerApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
            
        }
    }
}
1

There are 1 best solutions below

0
GgG On

I solved the problem with this

struct markdownFileView: View {
    
    @State var textArray: [String] = []
    var body: some View {
        ScrollView{
            VStack{
                ForEach(textArray, id: \.self) { text in
                    Text(try! AttributedString(
                        markdown: text,
                                        options: AttributedString.MarkdownParsingOptions(
                                            interpretedSyntax: .inlineOnlyPreservingWhitespace
                                        ))).textSelection(.enabled)

 
                }
            }
           
        }.onAppear{
            let filepath = Bundle.main.url(forResource: "test", withExtension: "md")
            let text = try! String(contentsOf: filepath!)
            let forCount = text.count/50000
            for i in 0...forCount {
                var to = (i+1)*50000
                let form = i*50000
                if to>text.count{
                    to = text.count
                }
                textArray.append(text[form..<to])
            }
        }
    }
}