Unable to wrap multiline text from UITextView via UIViewRepresentable in SwiftUI

479 Views Asked by At

I have a UITextView via a UIViewRepresentable which should be able to display multiline text, but unfortunately, the text distorts the entire view by remaining in one single line and clipping out of bounds. I managed to get the view back to normal, by using:

.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

This compresses the text, but keeps it still at a single line, and basically cuts it off instead of wrapping it to make it appear multi-line.

This is the UITextView:

struct SwiftUIView: UIViewRepresentable {
    
    var caption : String //The text being passed which may in some cases required to be multi-line
    


func makeUIView(context: Context) -> AttrTextView {
    
    
    
    let atview = AttrTextView()

    atview.isScrollEnabled = false
    atview.isEditable      = false
    atview.isSelectable    = false
    
    atview.setText(text: caption, withHashtagColor: UIColor.red, andMentionColor: UIColor.blue, andCallBack: callBack, normalFont: UIFont.systemFont(ofSize: 16), hashTagFont: UIFont.systemFont(ofSize: 16), mentionFont: UIFont.systemFont(ofSize: 16))
    atview.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) // Stops view from being distorted and strecthed out, though it cuts off the text instead
    
    atview.delegate          = atview

    return atview
}
    
    func updateUIView(_ uiView: AttrTextView, context: Context) {
        
    }
    
    func callBack(string: String, wordType: wordType){
        print("Recieved string: ", string)
        if wordType == .mention{
            print("mention")
        } else {
            print("other")
        }
    }


}

The AttrTextView() is a reference to the class which is still a UITextView, I just use it for attributed string modifications.

Then in another view, I call it as such:

SwiftUIView(caption: refSnippet.snippet.caption)

refSnippet.snippet.caption is what the text would be.

What should I do to get the view to wrap the text into multi-line?

0

There are 0 best solutions below