I am currently having an issue in where whenever I place a UITextView with text that's long, it simply pushes past the width of the container it is in, and when I do try and set a width, it does auto wrap and ignores that. I do not come from UIKit and come from SwiftUI, through a mix of ChatGPT and Stack overflow, I have come up with this solution, however, are there any better/simpler ones, I dont want to have to use expensive GeoReaders just to get a width.
struct AutoDetectedPhoneNumberView: UIViewRepresentable {
let text: String
var width: CGFloat
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.dataDetectorTypes = [.phoneNumber]
textView.isEditable = false
textView.isScrollEnabled = false
textView.backgroundColor = .clear
textView.font = UIFont.systemFont(ofSize: 16)
textView.textContainer.lineBreakMode = .byWordWrapping
textView.translatesAutoresizingMaskIntoConstraints = false
print(width)
NSLayoutConstraint.activate([
textView.widthAnchor.constraint(equalToConstant: width)
])
return textView
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text
}
}
GeometryReader { geo in
AutoDetectedPhoneNumberView(text: phone, width: geo.size.width)
}