Detecting taps on attributed text in a UITextView in iOS
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
UITextView can display rich text, links, and custom formatting, but tap handling is not automatic for every attributed range. The best solution depends on whether you are dealing with real links or with arbitrary highlighted text that should behave like a button.
Use Link Attributes When Possible
If the tapped text represents a URL, email address, or app-specific action, the simplest approach is to add the .link attribute to that range. UITextView already knows how to detect taps on links, and you can intercept them through the delegate.
This is the cleanest option because UITextView handles layout, hit testing, and accessibility for you.
Detect Taps On Arbitrary Attributed Ranges
Sometimes the text is not a real link. You may want a colored username, a mention, or a highlighted phrase to trigger a custom action. In that case, add a tap gesture recognizer and convert the tap location into a character index.
The core idea is:
- build
NSTextStorage,NSLayoutManager, andNSTextContainer - translate the tap point into text-container coordinates
- ask the layout manager for the tapped character index
- check whether that index falls inside the attributed range you care about
This works well for custom actions because you control the attributed range, not just visible styling.
Configure The Text View Properly
Several UITextView properties affect gesture behavior:
- set
isEditable = falseunless the user should type into the view - set
isSelectable = truewhen you rely on built-in link interactions - set
isSelectable = falsewhen you use your own recognizer and want to avoid selection handles - make sure the text view has its final frame before translating tap points
In practice, developers often get false negatives not because the index calculation is wrong, but because scrolling, padding, or selection settings were ignored.
Common Pitfalls
The biggest mistake is styling a range to look like a link and assuming it becomes tappable automatically. Visual styling alone does not create interaction.
Another common issue is forgetting about text-container padding or insets when mapping touch locations to character indexes. That shifts the hit test and makes taps look unreliable.
Developers also often leave the text view editable when they really want interaction-only behavior. The result is text-selection behavior competing with your tap handling.
Finally, if the content is truly a link, do not reimplement Text Kit hit testing unnecessarily. The built-in .link path is simpler and more robust.
Summary
- Use
.linkattributes and the text view delegate when the tappable text behaves like a link. - For arbitrary attributed ranges, map the tap point to a character index with Text Kit.
- Configure
UITextViewediting and selection behavior to match your interaction model. - Account for layout details such as padding and final bounds when hit testing.
- Prefer the built-in link path unless you genuinely need custom tap targets.

