By clicking on NSURL in a UITextView

I have a UITextView that spans 100.0 points in my UIView .

In UITextView , I have links that are captured using the following function:

 - (BOOL) textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange 

This works great for capturing certain characters, but I have one problem: if the link is the last characters in the text view, then clicking will be pressed all the way through this line.

So, if I have a 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 to fix it?

+5
source share
3 answers

For me, this only emphasizes the link ... Am I missing something?

enter image description here

Update:

Here is a really hacky solution using dummy URLs:

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] init]; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Lorem ipsum dolor sit amet, vim iuvaret blandit intellegebat ut. Solet diceret interpretaris eos cu, magna dicat explicari mei ex, cibo adversarium eu pro. Ei odio saepe eloquentiam cum, nisl case nec ut. Harum habemus definiebas et vix, est cu aeque sonet, in his salutatus repudiare deterruisset. Quo duis autem intellegat an, regione propriae et vis."]]; NSAttributedString* dummyUrl = [[NSAttributedString alloc] initWithString:@" " attributes:@{ NSLinkAttributeName : @"http://dummy.com" }]; NSAttributedString* url = [[NSAttributedString alloc] initWithString:@"http://stackoverflow.com" attributes:@{ NSLinkAttributeName : @"http://stackoverflow.com" }]; [attributedString appendAttributedString:dummyUrl]; [attributedString appendAttributedString:url]; [attributedString appendAttributedString:dummyUrl]; self.textView.attributedText = attributedString; } - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { return ![URL.absoluteString isEqualToString:@"http://dummy.com"]; } 

Basically you force UITextView recognize the crane before and after the stackoverflow link as a dummy link. Since this is just the space that it is invisible, however, unfortunately, if you touch and hold before / after the stackoverflow link, you will see shouldInteractWithURL out space, despite the fact that shouldInteractWithURL returns NO . Unfortunately, it seems that you cannot get around this behavior unless you implement your own UITextField from scratch ...

+2
source

As Tamás Zahola suggested in the previous answer, you can fix this problem by adding the “dummy” URL before and after the real URL. But instead of using a space in a dummy link, use a Unicode width character with zero width:

 NSAttributedString* dummyUrl = [[NSAttributedString alloc] initWithString:@"\u200B" attributes:@{ NSLinkAttributeName : @"http://dummy.com" }]; [attributedString appendAttributedString:dummyUrl]; [attributedString appendAttributedString:url]; [attributedString appendAttributedString:dummyUrl]; 
+2
source

enter image description here You just need to set some properties after the link that textView is for encoding, for example, for the delegate textview, editable and dataDetectorTypes

Below I added the content that I used

Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tem incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud, carrying out work carried out in accordance with needs. https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson3.html Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. /fooobar.com / ... Excepteur sint occaecat cupidatat is irrelevant, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscientious to factor tum poen legum odioque civiuda /fooobar.com / ...

for objective-c

 textView.delegate=self: textView.editable = NO; textView.dataDetectorTypes = UIDataDetectorTypeLink; - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { return true; 

}

for swift

 textView1.delegate=self textView1.editable = false textView1.dataDetectorTypes = UIDataDetectorTypes.Link func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool { return true } 
0
source

Source: https://habr.com/ru/post/1240750/


All Articles