I have a UITextView
that spans 100.0 points across my UIView
.
In the UITextView
, I have links that get captured with the following function:
- (BOOL) textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
This works great to capture certain characters but I have one problem: if the link is the last characters in the text view, then the tap will get pressed all the way across that line.
So if I have the text view with the following text where @test is the link:
// The entire remainder of the line will be the link (all the white space after @test)
Hello @test
How do I fix this?
For me it only highlights the link... Am I missing something?
Update:
Here's a really hacky solution via dummy URL's:
Basically you force the
UITextView
to recognise the tap before and after the stackoverflow link as the dummy link. Since it's just a space it's invisible, however unfortunately if you tap and hold before/after the stackoverflow link you'll see the space highlighted with gray, despiteshouldInteractWithURL
returningNO
. Unfortunately it seems you cannot circumvent this behaviour unless you implement your ownUITextField
from scratch...You simply need to set some properties after linking that textView to code like textview delegate, editable and dataDetectorTypes
Below i have added the content i have used
for objective-c
}
for Swift
As Tamás Zahola suggested in previous answer, you can fix this issue by adding "dummy" URLs before and after real URL. But, instead using whitespace in dummy link, use zero width space unicode character: