AttributedText icon indicating copy to clipboard operation
AttributedText copied to clipboard

Add a way to handle interaction with URLs

Open Tunous opened this issue 3 years ago • 2 comments

Description

This pull request introduces possibility to override default behavior when interacting with URLs located inside of NSAttributedString. My use case is that I want to handle URLs in a specific way instead of always redirecting the user to browser. Additionally I want to distinguish between tap and long tap interactions on these links. This pull request makes all of that possible.

Implementation

The feature is implemented by addition of optional openURL parameter. If developers do not specify it AttributedText component will behave like previously. If developers pass that block, they can run custom code each time users interact with URLs.

The meaning of return value from openURL parameter varies between platform and has been documented accordingly based on documentation from func textView(_: UITextView, shouldInteractWith URL: URL, in _: NSRange, interaction: UITextItemInteraction) -> Bool and func textView(_: NSTextView, clickedOnLink link: Any, at _: Int) -> Bool functions.

I didn't add any new tests as this library only contains snapshot tests, which don't work when wanting to test interaction with links.

Example

Here by returning true on iOS it's now possible for users to long press on links to show default actions list:

AttributedText(attributedString) { url, interaction in
    // Allow performing default action
    return true
}

image

Tunous avatar Apr 03 '21 13:04 Tunous

I want this feature too! 👍

ikesyo avatar Sep 13 '21 10:09 ikesyo

This would partially fix #7.

ikesyo avatar Sep 13 '21 12:09 ikesyo