程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何根据文本视图内容 Swiftui 管理 Scrollview 高度大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何根据文本视图内容 Swiftui 管理 Scrollview 高度?

开发过程中遇到如何根据文本视图内容 Swiftui 管理 Scrollview 高度的问题如何解决?下面主要结合日常开发的经验,给出你关于如何根据文本视图内容 Swiftui 管理 Scrollview 高度的解决方法建议,希望对你解决如何根据文本视图内容 Swiftui 管理 Scrollview 高度有所启发或帮助;

我正在尝试根据 textvIEw 内容高度与其他视图管理滚动视图高度。

代码:

struct TextvIEwWithScrollvIEw: VIEw {
    @State private var textStyle = UIFont.TextStyle.body
    @State private var textForTextVIEw = "fdgfhjdsgfdhsgfdsfg dsfg dsfgdsfh fh sf hdsjklf dhsjkfhsdjkfdhsjkfadhsfkds fdshfjkldsh fjkldsh fdshfdshfdsfhsfdsfh sf ewf g iuf herjkdsjkjvdhsvdshvdsv dshv ds vads vds hvsdvjkds vds hviuds  vhv disu ghdisuv g"
    var body: some VIEw {
        ZStack {
            color.red
                .ignoresSafeArea()
            ScrollVIEw {
                vstack(alignment: .leading) {
                    TextVIEw(isEditable: .constant(false),text:  .constant(textForTextVIEw),textStyle:$textStyle,dIDStartEdiTing: .constant(false),placeHolderText:"")
                }
                
                HStack {
                    button("top") {
                    }
                    button("MIDdle") {
                        
                    }
                    button("Bottom") {
                    }
                }
            }
        }
    }
}

struct TextVIEw: UIVIEwRepresentable {
    @Binding var isEditable:Bool
    @Binding var text: String
    @Binding var textStyle: UIFont.TextStyle
    @Binding var dIDStartEdiTing: Bool
    var placeHolderText: String = ""
    
    
    func makeUIVIEw(context: Context) -> UITextVIEw {
        let textVIEw = UITextVIEw()
        textVIEw.delegate = context.coordinator
        textVIEw.Font = UIFont.preferredFont(forTextStyle: textStylE)
        textVIEw.autocAPItalizationType = .sentences
        textVIEw.isSELEctable = true
        textVIEw.isUserInteractionEnabled = true
        textVIEw.isScrollEnabled = true
        textVIEw.dataDetectorTypes = .all
        textVIEw.textcolor = .white
        return textVIEw
    }
    
    func updateUIVIEw(_ uiVIEw: UITextVIEw,context: Context) {
        uiVIEw.text = text
        uiVIEw.Font = UIFont.preferredFont(forTextStyle: textStylE)
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator($text)
    }
    
    class Coordinator: NSObject,UITextVIEwDelegate {
        var text: Binding<String>
        
        init(_ text: Binding<String>) {
            self.text = text
        }
        
        func textVIEwDIDChange(_ textVIEw: UITextVIEw) {
            dispatchQueue.main.async {
                self.text.wrappedValue = textVIEw.text
            }
        }
    }
}

没有滚动视图的输出:

如何根据文本视图内容 Swiftui 管理 Scrollview 高度

使用滚动视图输出:

如何根据文本视图内容 Swiftui 管理 Scrollview 高度

有人可以向我解释如何根据文本视图内容和其他视图来管理滚动视图高度。我已经尝试通过上面的方法实现,但还没有结果。

任何帮助将不胜感激。

提前致谢。

解决方法

如果我理解正确,这就是您想要实现的目标:

如何根据文本视图内容 Swiftui 管理 Scrollview 高度

要获得此结果,请将 ScrollView 嵌入 GeometryReader 并使用框架调整 TextView 的大小。像这样:

struct TextviewWithScrollview: View {
@State private var textStyle = UIFont.TextStyle.body
@State private var textForTextView = "fdgfhjdsgfdhsgfdsfg dsfg dsfgdsfh fh sf hdsjklf dhsjkfhsdjkfdhsjkfadhsfkds fdshfjkldsh fjkldsh fdshfdshfdsfhsfdsfh sf ewf g iuf herjkdsjkjvdhsvdshvdsv dshv ds vads vds hvsdvjkds vds hviuds  vhv disu ghdisuv g"
var body: some View {
    ZStack {
        Color.red
            .ignoresSafeArea()
        GeometryReader { geo in
            ScrollView {
                VStack(alignment: .leading) {
                    TextView(isEditable: .constant(false),text:  .constant(textForTextView),textStyle:$textStyle,didStartEdiTing: .constant(false),placeHolderText:"")
                        .frame(width: geo.size.width,height: 300)
                }
                HStack {
                    Button("Top") {
                    }
                    Button("Middle") {
                    }
                    Button("Bottom") {
                    }
                }
            }
        }
    }
}
,

这是另一种根据显示的文本数量自动调整 TextView 大小的解决方案:

如何根据文本视图内容 Swiftui 管理 Scrollview 高度

这是代码:

class myTextObject: ObservabLeobject {
    @Published var text: String = "Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes,nasCETur riDiculus mus. Donec quam felis,ultricies nec,pellentesque eu,pretium quis,sem."
}

struct TextviewWithScrollview: View {
    @State private var textStyle = UIFont.TextStyle.body
    
    @StateObject var textForView = myTextObject()
    @State private var textViewSize: CGSize = CGSize(width: 300,height: 300)
    
    var body: some View {
        ZStack {
            Color.red
                .ignoresSafeArea()
            
            GeometryReader { geo in
                ScrollView {

                    VStack(alignment: .leading) {
                        TextView(
                            isEditable: .constant(false),text: $textForView.text,textStyle: $textStyle,placeHolderText: "",textViewSize: $textViewSize
                        )
                        .frame(minHeight: 30,idealHeight: min(textViewSize.height,geo.size.height),maxHeight: geo.size.height)
                    }
                    
                    HStack {
                        Button("Top") {
                        }
                        Button("Middle") {
                            self.$textForView.text.wrappedValue += "\n-- a new line --"
                        }
                        Button("Bottom") {
                        }
                    }
                }
            }
        }
    }
}

struct TextView: UIViewRepresentable {
    @Binding var isEditable:Bool
    @Binding var text: String
    @Binding var textStyle: UIFont.TextStyle
    @Binding var didStartEdiTing: Bool
    var placeHolderText: String = ""
    
    @Binding var textViewSize: CGSize
    
    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        textView.delegate = context.coordinator
        textView.font = UIFont.preferredFont(forTextStyle: textStylE)
        textView.autocapitalizationType = .sentences
        textView.isSELEctable = true
        textView.isUserInteractionEnabled = true
        textView.isScrollEnabled = true
        textView.dataDetectorTypes = .all
        textView.textColor = .gray
        DispatchQueue.main.async {
            textViewSize = textView.sizeThatFits(textView.textContainer.sizE)
        }
        return textView
    }
    
    func updateUIView(_ uiView: UITextView,context: Context) {
        uiView.text = text
        uiView.font = UIFont.preferredFont(forTextStyle: textStylE)
        
        DispatchQueue.main.async {
            let newContentSize = uiView.sizeThatFits(uiView.textContainer.sizE)
            context.coordinator.parent.$textViewSize.wrappedValue = newContentSize
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(parent: self)
    }
    
    class Coordinator: NSObject,UITextViewDelegate {
        var parent: TextView
        
        init(parent: TextView)  {
            self.parent = parent
        }
        
        func textViewDidChange(_ textView: UITextView) {
            
            DispatchQueue.main.async {
                self.parent.$text.wrappedValue = textView.text
                
                let newContentSize = textView.sizeThatFits(textView.textContainer.sizE)
                self.parent.$textViewSize.wrappedValue = newContentSize
            }
        }
    }
}

,

将宽度约束设置为文本视图并在视图顶部设置 GeometryReader

顶部设置GeometryReader的原因,因为在滚动视图内部它不允许完全滚动。

UIViewRepresentable Text View

struct TextView: UIViewRepresentable {
    var attributedString: NSAttributedString
    var fixedWidth: CGFloat
    
    let textView = UITextView(frame: .zero)
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
    
    
    class Coordinator: NSObject,UITextViewDelegate {
        var parent: TextView
        init(_ parent: TextView) {
            self.parent = parent
        }
    }
    
    func makeUIView(context: Context) -> UITextView {
        textView.isEditable = true
        textView.isScrollEnabled = false
        textView.BACkgroundColor = .cyan // just to make it easier to see
        textView.delegate = context.coordinator
        textView.translatesAutoresizingMaskIntoConsTraints = false
        textView.widthAnchor.consTraint(equalToConstant: fixedWidth).isActive = true //<--- Here
        return textView
    }
    
    func updateUIView(_ textView: UITextView,context: Context) {
        DispatchQueue.main.async { //<--- Here
            textView.attributedText = self.attributedString
        }
    }
}

ContentView

struct ContentView: View {
    
    @State private var textViewSize: CGSize = CGSize()
    
    var body: some View {
        GeometryReader { geo in //<--- Here
            ScrollView(.vertical) {
                VStack(alignment: .leading,spacing: 0){
                    //Other content
                    Image(systemname: "photo")
                        .font(.largetitlE)
                    Button("A button"){
                        print("i'm a button")
                    }
                    
                    TextView(attributedString: NSAttributedString(String: "Here is a long String as you can see it is not wrapping but instead scrolling off the edge of the screenHere is a long String as you can see it is not wrapping but instead scrolling off the edge of the screenHere is a long String as you can see it is not wrapping but instead scrolling off the edge of the screenHere is a long String as you can see it is not wrapping but instead scrolling off the edge of the screenHere is a long String as you can see it is \n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new line\n1new lineinstead scrolling off the edge of the screenHere is a long String as you can see it is not wrapping but instead scrolling off the edge of the screenHere is a long String as you can see it is not wrapping but instead scrolling off the edge of the screen ------end"),fixedWidth: geo.size.width - 15 * 2) //<--- Here minus the padding for both size
                }.padding(.horizontal,15)
            }
        }
    }
}

如何根据文本视图内容 Swiftui 管理 Scrollview 高度

大佬总结

以上是大佬教程为你收集整理的如何根据文本视图内容 Swiftui 管理 Scrollview 高度全部内容,希望文章能够帮你解决如何根据文本视图内容 Swiftui 管理 Scrollview 高度所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。