Skip to content

Instantly share code, notes, and snippets.

@aheze
Created October 16, 2024 18:34
Show Gist options
  • Save aheze/44761be4e999ce2e20596f5cfd9cd0c3 to your computer and use it in GitHub Desktop.
Save aheze/44761be4e999ce2e20596f5cfd9cd0c3 to your computer and use it in GitHub Desktop.
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, world!")
.frame(maxWidth: .infinity, maxHeight: .infinity)
ResizableTextView()
.border(.red)
}
.padding()
}
}
// this doesn't work when the text is wrapped
struct ResizableTextView: UIViewRepresentable {
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.font = UIFont.preferredFont(forTextStyle: .title1)
// fit content
textView.isScrollEnabled = false
textView.setContentHuggingPriority(.defaultHigh, for: .vertical)
// prevent expanding horizontally
textView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
return textView
}
func updateUIView(_ uiView: UITextView, context: Context) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment