gonzalezreal / swift-markdown-ui

Display and customize Markdown text in SwiftUI
MIT License
2.26k stars 267 forks source link

Disabling text selection #51

Closed markiv closed 2 years ago

markiv commented 3 years ago

Hola @gonzalezreal

Is there a way to disable text selection without disabling links?

Here's my use-case:

image

gonzalezreal commented 3 years ago

The Markdown SwiftUI view is backed by a UITextView in iOS, which has a selectable property that controls the ability to both select text and interact with URLs and text attachments.

A possible solution would be to implement a custom UIView subclass that renders the attributed text and handles links, but that can be very difficult (text layout is complex) and definitely out of the scope of this library.

markiv commented 3 years ago

You're right. Pity. isSelectable applies to both text selection and links.

@available(iOS 7.0, *)
open var isSelectable: Bool // toggle selectability, which controls the ability of the user to select content and interact with URLs & attachments. On tvOS this also makes the text view focusable.

There's a possible workaround by overriding point(inside:with:): https://stackoverflow.com/questions/36198299/uitextview-disable-selection-allow-links

956MB commented 2 years ago

A workaround I've found is putting the Markdown in a ZStack with a clear View above it:

VStack(alignment: .leading, spacing: 2) {
    ZStack {
        Markdown(self.cellContent)
            .lineLimit(4)
            .multilineTextAlignment(.leading)
            .truncationMode(.tail)
            .allowsTightening(true)
            .accentColor(self.accent)

        HStack {  // << clear view
        }
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
        .background(Color(hex: 0xFFFFFF).opacity(0.001))
    }
}

Reason for .opacity(0.001) is because for some reason TapGesture input im using for single/double clicks is not registered when using Color.clear or .opacity(0.0) on this view.

This seems to solve the problem of text selection on the markdown, since there doesnt seem to be a simple modifier for it, however you still have the "I-beam" text selection style cursor when hovering it, but maybe theres a way to change the cursor over certain views.

EDIT: I realized I wasn't addressing the without disabling links part of your issue. Links do not work in this workaround unforunately, just the removal of text selection.