iOS 7 中的 UITextView 链接检测

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18962742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:35:34  来源:igfitidea点击:

UITextView link detection in iOS 7

iosobjective-cios6uitextviewios7

提问by Tobias

I have a UITextViewwhich is managed via Interface Builder. As data detection I have "Links" checked. In iOS 6 everything is working fine and links are highlighted and are clickable. In iOS 7 though, all links remain just plain text. The editable and selectable checkboxes are unchecked.

我有一个UITextView通过 Interface Builder 管理的。作为数据检测,我检查了“链接”。在 iOS 6 中一切正常,链接突出显示并且可以点击。但在 iOS 7 中,所有链接都只是纯文本。可编辑和可选择的复选框未选中。

What may be of concern is that the UITextViewis a subview of a container view which is again inside a UIScrollView.

可能需要关注的是UITextView是容器视图的子视图,它再次位于UIScrollView.

回答by Tobias

It seems that in iOS 7 link detection only works if the UITextViewis selectable. So making my UITextViewnot selectable stopped the the link detection from working.

似乎在 iOS 7 中链接检测仅在UITextView可选时才有效。因此,使我的UITextView不可选择停止了链接检测的工作。

I also tested this in iOS 6 and I can confirm that in iOS 6 the link detection works fine even with the UITextViewnot being selectable.

我还在 iOS 6 中对此进行了测试,我可以确认在 iOS 6 中,即使UITextView无法选择链接检测也能正常工作。

回答by Theo Bendixson

I was having some problems with phone number detection today. It seemed like the UITextView would retain old phone numbers and keep text highlighted after I had set the text to something else.

我今天在电话号码检测方面遇到了一些问题。在我将文本设置为其他内容后,UITextView 似乎会保留旧的电话号码并突出显示文本。

I found that if I setText:nil before setting the text to the new string, it would reset the textview, and phone numbers would highlight as normal. I'm wondering if this is some kind of bug with UITextView in iOS 7.0

我发现如果我在将文本设置为新字符串之前 setText:nil ,它会重置文本视图,并且电话号码会正常突出显示。我想知道这是否是 iOS 7.0 中 UITextView 的某种错误

Either way, this did work for me.

无论哪种方式,这都对我有用。

回答by Raconteur

When iOS7 first came out this plagued me and I found an answer in this thread (setting the text attribute of the UITextView to nil before setting the actual value did the trick). Then suddenly, the problem (for me it was the entire string being highlighted as a link) cropped back up (assumedly due to an iOS update).

当 iOS7 第一次出现时,这困扰着我,我在这个线程中找到了答案(在设置实际值之前将 UITextView 的文本属性设置为 nil 可以解决问题)。然后突然,问题(对我来说是整个字符串被突出显示为链接)又出现了(假设是由于 iOS 更新)。

What finally did the trick for me was to stop using the text attribute and set the attributedText. Once I did this, the need for setting fonts/scrolling/selectable/editable/etc. programmatically, disappeared. I defined my UITextView in IB, set the values as I wanted (not scrollable, not editable, selectable, detecting links and phone numbers) and then built an attributed string and set:

最终对我有用的是停止使用文本属性并设置属性文本。一旦我这样做了,需要设置字体/滚动/可选择/可编辑/等。以编程方式,消失了。我在 IB 中定义了我的 UITextView,根据需要设置值(不可滚动、不可编辑、可选择、检测链接和电话号码),然后构建一个属性字符串并设置:

myUITextView.attributedString = myAttributedString;

And suddenly everything worked as expected. Hope this helps someone else down the road.

突然间,一切都按预期进行。希望这可以帮助其他人。

回答by svarrall

I had the same issue and disabling scrolling on the UITextView activates the link detection on load rather than only working once the user has interacted with the textview. The UITextView also had to be selectable and non-editable.

我遇到了同样的问题,在 UITextView 上禁用滚动会在加载时激活链接检测,而不仅仅是在用户与 textview 交互后才工作。UITextView 还必须是可选择和不可编辑的。

detailTextView.scrollEnabled = NO;
detailTextView.editable = NO;
detailTextView.selectable = YES;

Being selectable or having scroll enabled isn't necessary on iOS6.

在 iOS6 上不需要可选择或启用滚动。

Another thing to check is that userinteraction is enabled on the cell and content view of the cell, otherwise the link won't be clickable.

要检查的另一件事是在单元格和单元格的内容视图上启用了用户交互,否则链接将不可点击。

回答by Harish Pathak

Check These Lines must be added to use data detector property of textviewin UItableViewcell.

检查这些行必须添加使用的数据检测器性能textviewUItableView细胞。

    txtvwMsgText.userInteractionEnabled = YES;
    txtvwMsgText.dataDetectorTypes = UIDataDetectorTypeLink;
    txtvwMsgText.scrollEnabled = NO;
    txtvwMsgText.editable = NO;
    txtvwMsgText.selectable = YES;

回答by Daniel

You should check out NSDataDetector.

你应该检查一下NSDataDetector

You can use this to find and deal with different data (links, phone numbers and more). Have a look on this site:

您可以使用它来查找和处理不同的数据(链接、电话号码等)。看看这个网站:

http://nshipster.com/nsdatadetector/

http://nshipster.com/nsdatadetector/

You can also use the dataDetectorTypesproperty of UITextViewto set what you want to detect in code. May just be a storyboard transition problem for you.

您还可以使用dataDetectorTypesof 属性UITextView来设置要在代码中检测的内容。可能只是你的故事板过渡问题。

textView.dataDetectorTypes = UIDataDetectorTypeLink;

回答by Alexander

Be aware, that your textview will only recognize the links if not editable!

请注意,如果不可编辑,您的 textview 只会识别链接

Here is a nice tutorial on how to make an editable UITextViewwith `link detection``

这是一个关于如何editable UITextView使用“链接检测”制作一个很好的教程

Editable UITextView with link detecion

具有链接检测功能的可编辑 UITextView

I've not experienced any problems with that solution since now.

从现在起,我就没有遇到过该解决方案的任何问题。

The trick is a GestureRecognizerforwaring touches and enabling/disabling the editing.

诀窍是GestureRecognizer转发触摸和启用/禁用编辑。

You could apply the same thing with the selectable / not selectableissue on iOS7

你可以对这个selectable / not selectable问题应用同样的东西iOS7

回答by Kubík Ka?par

After few tests, I found solution.

经过几次测试,我找到了解决方案。

If you want links active and you won't selection enabled, you need to edit gestureRecognizers.

如果您希望链接处于活动状态并且不启用选择,则需要编辑gestureRecognizers。

For example - there are 3 LongPressGestureRecognizers. One for click on link (minimumPressDuration = 0.12), second for zoom in editable mode (minimumPressDuration = 0.5), third for selection (minimumPressDuration = 0.8). This solution removes LongPressGestureRecognizer for selecting and second for zooming in editing mode.

例如 - 有 3 个 LongPressGestureRecognizers。第一个用于单击链接(minimumPressDuration = 0.12),第二个用于放大可编辑模式(minimumPressDuration = 0.5),第三个用于选择(minimumPressDuration = 0.8)。此解决方案删除了​​用于选择的 LongPressGestureRecognizer 和用于放大编辑模式的第二个。

NSArray *textViewGestureRecognizers = self.captionTextView.gestureRecognizers;
NSMutableArray *mutableArrayOfGestureRecognizers = [[NSMutableArray alloc] init];
for (UIGestureRecognizer *gestureRecognizer in textViewGestureRecognizers) {
    if (![gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
        [mutableArrayOfGestureRecognizers addObject:gestureRecognizer];
    } else {
        UILongPressGestureRecognizer *longPressGestureRecognizer = (UILongPressGestureRecognizer *)gestureRecognizer;
        if (longPressGestureRecognizer.minimumPressDuration < 0.3) {
            [mutableArrayOfGestureRecognizers addObject:gestureRecognizer];
        }
    }
}
self.captionTextView.gestureRecognizers = mutableArrayOfGestureRecognizers;

Tested on iOS 9, but it should work on all versions (iOS 7, 8, 9). I hope it helps! :)

在 iOS 9 上测试过,但它应该适用于所有版本(iOS 7、8、9)。我希望它有帮助!:)

回答by skypirate

So using a UITextView keeping it enabled, selectable, not scrollable & links detectable is not as simple as it seems. I encountered this in iOS 8. So my solution was to do something like this in viewDidLoad and then set editable property to NO when textBox editing is done(usually would be a method like doneIsTapped). The trick here is to set editable property to NO after setting text value to textview is completed. This will enable links in the UITextview.

因此,使用 UITextView 保持启用、可选择、不可滚动和可检测链接并不像看起来那么简单。我在 iOS 8 中遇到了这个问题。所以我的解决方案是在 viewDidLoad 中做这样的事情,然后在 textBox 编辑完成时将 editable 属性设置为 NO(通常是像 doneIsTapped 这样的方法)。这里的技巧是在将文本值设置为 textview 完成后将 editable 属性设置为 NO。这将启用 UITextview 中的链接。

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.txtViewComment.editable = YES;
    self.txtViewComment.selectable = YES;
    self.txtViewComment.dataDetectorTypes = UIDataDetectorTypeLink;
    self.txtViewComment.scrollEnabled = NO;
}

and

- (IBAction)doneIsTapped:(id)sender 
{
    self.txtViewComment.text = @"set text what ever you want";
    self.txtViewComment.editable = NO; 
}

this made the links enabled in textview. Also I would recommend not to use story board at this time(or until apple fixes this problem) and just use code to avoid any unnecessary confusion. Hope this help.

这使得在 textview 中启用链接。此外,我建议此时不要使用故事板(或在苹果解决此问题之前),只使用代码来避免任何不必要的混淆。希望这有帮助。

回答by álvaro Murillo

I've found the trick, this works in iOS 7!

我找到了诀窍,这适用于 iOS 7!

You have to set the UITextView selectable in your xib or programmatically

您必须在 xib 中或以编程方式设置 UITextView 可选

self.yourTextView.selectable = YES;

and then you have to disable scrolls and enable again after set your text.

然后您必须禁用滚动并在设置文本后再次启用。

self.yourTextView.scrollEnabled = NO;
[self.yourTextView setText:contentString];
self.yourTextView.scrollEnabled = YES;