Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

ios - Clicking on NSURL in a UITextView

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For me it only highlights the link... Am I missing something?

enter image description here

Update:

Here's a really hacky solution via dummy URL's:

- (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 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, despite shouldInteractWithURL returning NO. Unfortunately it seems you cannot circumvent this behaviour unless you implement your own UITextField from scratch...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...