It appears to me that it is possible to use UITextView instead of Text. And with that, you end up with a scrollable element (interestingly completely without using scrollview.
Note, in the example below I also used attributified text to render html elements for styling. So even that works. But it also works with just a regular text string!
Rectangle()
.fill(.ultraThinMaterial)
.ignoresSafeArea()
FocusableAttributedTextView(attributedString: viewModel.attributedString)
.frame(width: 500,
height: 800)
.background(.ultraThinMaterial)
.clipShape(RoundedRectangle(cornerRadius: Constants.cornerRadius))
.focused($isFocused)
}
.onExitCommand {
showExpanded = false
}
struct FocusableAttributedTextView: UIViewRepresentable {
let attributedString: AttributedString
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
// Configure for tvOS focus and scrolling as per Cocoanetics/DTCoreText#1149
textView.isSelectable = true
textView.isUserInteractionEnabled = true
textView.panGestureRecognizer.allowedTouchTypes = [NSNumber(value: UITouch.TouchType.indirect.rawValue)]
textView.font = UIFont.systemFont(ofSize: 90)
return textView
}
func updateUIView(_ uiView: UITextView, context: Context) {
// Convert SwiftUI AttributedString to NSAttributedString for UITextView
let nsAttributedString = NSAttributedString(attributedString)
uiView.attributedText = nsAttributedString
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: