繁体   English   中英

iOS UITextView链接检测

[英]iOS UITextView Link detection

我有一个选择了“检测Links的UITextView, selectable and editable未选中“行为selectable and editable 它检测到前缀为“ www或http://或https://等”的链接,但不会检测到goole.co.in。 任何解决方案。

这可能不是您问题的解决方案,但我根据您的问题发现了一个奇怪的问题

对于前:

    txtTest.userInteractionEnabled = true;
    txtTest.selectable = true
    txtTest.dataDetectorTypes = .Link;
    txtTest.text = "stackoverflow.com"

当我编写此代码时,它会检测到textview中的链接,如果在其中将“ google.co.uk”,“ google.co.us”,“ google.com”中的任何一个写在了相同的情况下,它检测到链接的文本

但是正如您在问题中提到的,如果我们将文本更改为“ google.co.in”,则不会检测到链接,我尝试使用“ google.co.test”,这也不会检测到链接

因此,这可能与“ dataDetectorTypes”的默认链接检测模式有关,否则您的代码可能适用于其他网址

如果您有静态链接,则可以检查是否在textview中检测到此链接

否则,您可以尝试UIWebView(如果适用)

另外,如果苹果没有满足您的需求,请自己检查。 遍历UITextView的文本输出,查找特定的关键字,例如“ .co.in”,如果它们存在,则突出显示它们(或其他内容)。

NSString * text = textView.text;
if ([text containsString:".co.in"]){
   //Indian domain found
}

UPDATE

您可以使用attributedStrings高亮显示字符串中的特定文本:

NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:myString];
UIFont * boldFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f];
NSDictionary * attributedDict = [NSDictionary dictionaryWithObjectsAndKeys:boldFont, NSFontAttributeName, [UIColor blueColor], NSForegroundColorAttributeName, nil];
NSRange range = [myString rangeOfString:@"www.google.co.in"];
[attributedString setAttributes:attributedDict range:range];
[myLabel setAttributedText:attributedString];

关于触摸,可以在整个UILabel上放置轻击手势识别器,然后使用以下命令检测轻击的位置:

-(void)longPressDetected:(UILongPressGestureRecognizer *)sender {
    CGPoint touchLocation = [sender locationInView:myLabel];
}

然后从那里检查敲击位置是否与已知单词框架重叠。

我感觉可能有一种更简单的方法来实现您想要的工作,但是我不记得它是什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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