简体   繁体   中英

Attributed string click is not sensitive with UITextView

Clickable text is not sensitive when touching, need stay finger for a while to trigger that event. Is there a method to resolve that?

From our discussion in comments, I believe that you asked for a way to make part of String clickable. For this you can use NSAttributeString along with UITextView to resolve your problem.

NSString *myString = @"Sample string - click";
NSMutableAttributedString *myAttributedString = [[NSMutableAttributedString alloc] initWithString:myString];
[myAttributedString addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"randomString"] range:[initString rangeOfString:@"click"]];

UITextView *myNonEditableTextView = [[UITextView alloc] initWithFrame:CGRectMake(60, 200, [[UIScreen mainScreen] bounds].size.width - 120, 100)];
myNonEditableTextView.delegate = self;
myNonEditableTextView.attributedText = myAttributedString;
myNonEditableTextView.editable = NO;
[self.view addSubview: myNonEditableTextView];

// delegate functions for UITextView

// - For versions >= iOS 10, this delegate function should be used 
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
    [self performAction];
    return NO;
}

// - For versions < iOS 10, this delegate function should be used
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    [self performAction];
    return NO;
}

Hope this helps :) and let me know if you need a swift version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM