In iOS 26, the iMessages app has a function that permits choosing solely a part of the textual content. See the screenshots under. How can we obtain this sort of choice in SwiftUI? I attempted utilizing the .textSelection(.enabled) modifier, however it solely exhibits the Copy and Share choices.
Replace:
I attempted @Subh Karan’s resolution, however I’m unable to make the UITextView match its content material measurement. It at the moment takes up the complete width. Please see the picture under.
Right here is my present implementation
HStack {
Spacer(minLength: 40)
SelectableTextView(textual content: message.physique, textColor: .white)
.fixedSize(horizontal: false, vertical: true)
.body(minWidth: 0,
maxWidth: UIScreen.major.bounds.width * 0.75,
alignment: .trailing)
.padding(.horizontal)
}
struct SelectableTextView: UIViewRepresentable {
var textual content: String
var textColor: UIColor
func makeUIView(context: Context) -> UITextView {
let view = ChatTextView()
view.isEditable = false
view.isSelectable = true
view.isScrollEnabled = false
view.backgroundColor = .crimson
view.textContainerInset = .zero
view.textContainer.lineFragmentPadding = 0
view.font = UIFont.systemFont(ofSize: 17)
view.textColor = textColor
view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
view.setContentHuggingPriority(.required, for: .vertical)
return view
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.textual content = textual content
}
}
ultimate class ChatTextView: UITextView {
override var intrinsicContentSize: CGSize {
// Drive top based mostly on precise content material
let measurement = self.sizeThatFits(CGSize(width: self.bounds.width, top: .greatestFiniteMagnitude))
return CGSize(width: measurement.width, top: measurement.top)
}
}



