簡體   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