Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如果URL太大,UITextView中链接的自动检测将失败_Ios_Url_Hyperlink_Uilabel_Uitextview - Fatal编程技术网

Ios 如果URL太大,UITextView中链接的自动检测将失败

Ios 如果URL太大,UITextView中链接的自动检测将失败,ios,url,hyperlink,uilabel,uitextview,Ios,Url,Hyperlink,Uilabel,Uitextview,我当前的URL长度为801个字符。我发现,如果URL长度超过350个字符,在UITextView中自动检测链接是不起作用的。此外,我还在“Messages”应用程序中测试了我的链接,但它不起作用(消息将使用UILabel或UITextView显示文本内容)。我们有没有办法检测长度超过350个字符的URL 如果UITextView的数据检测器无法检测到长度超过350个字符的URL,我认为您可以使用正则表达式自己检测。检测到URL后,可以使用attributedString将其设置为UITextVi

我当前的URL长度为801个字符。我发现,如果URL长度超过350个字符,在UITextView中自动检测链接是不起作用的。此外,我还在“Messages”应用程序中测试了我的链接,但它不起作用(消息将使用UILabel或UITextView显示文本内容)。我们有没有办法检测长度超过350个字符的URL

如果UITextView的数据检测器无法检测到长度超过350个字符的URL,我认为您可以使用正则表达式自己检测。检测到URL后,可以使用attributedString将其设置为UITextView

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor grayColor];
    self.textView.textColor = [UIColor blackColor];
    self.textView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);

    NSString *testString = @"google dog meme  https://www.google.com.sg/search?q=dog+meme&espv=2&biw=1672&bih=890&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjWr8rexqXSAhUO9GMKHYHXD7QQ_AUIBigB#tbm=isch&q=doge+meme&imgrc=SFF-4BKQucOc4M: hhahaa";
    NSDataDetector *detect = [[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeLink error:nil];
    NSArray *matches = [detect matchesInString:testString options:0 range:NSMakeRange(0, [testString length])];

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:testString attributes:nil];
    NSTextCheckingResult *result = matches.firstObject;
    if (result) {
        [attributedString addAttribute:NSLinkAttributeName value:result.URL.absoluteString range:result.range];
    }
    self.textView.editable = NO;
    self.textView.attributedText = attributedString;
    self.textView.userInteractionEnabled = YES;
    self.textView.delegate = self;
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    NSLog(@"#### %@", URL);
    return YES;
}
另一种方法是使用URL缩短器先缩短URL,然后显示它们。我认为这在用户体验方面很有价值,因为用户可能不愿意点击一个很长的URL


我希望这能对你有所帮助。

Hmm,从我的角度来看,这很有效,你有什么问题吗?你能尝试使用任何超过350个字符的URL吗?。感谢url缩短器建议。使用
url长度1332
进行了测试,效果良好。我可以突出显示URL,它是可点击的。您是否设置了self.textView.editable=NO