14.5 C
Canberra
Thursday, February 12, 2026

The way to allow partial textual content choice in SwiftUI like iOS 26 iMessage


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.

The way to allow partial textual content choice in SwiftUI like iOS 26 iMessage

enter image description here

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.

enter image description here

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)
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles