简体   繁体   English

iOS 7中的UITextView链接检测

[英]UITextView link detection in iOS 7

I have a UITextView which is managed via Interface Builder. 我有一个UITextView ,它通过Interface Builder进行管理。 As data detection I have "Links" checked. 作为数据检测,我检查了“链接”。 In iOS 6 everything is working fine and links are highlighted and are clickable. 在iOS 6中,一切正常,链接突出显示且可点击。 In iOS 7 though, all links remain just plain text. 但是在iOS 7中,所有链接仍然只是纯文本。 The editable and selectable checkboxes are unchecked. 未选中可编辑和可选择的复选框。

What may be of concern is that the UITextView is a subview of a container view which is again inside a UIScrollView . 可能引起关注的是UITextView是容器视图的子视图,它再次位于UIScrollView

It seems that in iOS 7 link detection only works if the UITextView is selectable. 似乎在iOS 7中链接检测仅在UITextView可选时才有效。 So making my UITextView not selectable stopped the the link detection from working. 因此,使我的UITextView无法选择会阻止链接检测工作。

I also tested this in iOS 6 and I can confirm that in iOS 6 the link detection works fine even with the UITextView not being selectable. 我也在iOS 6中对此进行了测试,我可以确认在iOS 6中链接检测工作正常,即使UITextView不可选。

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. 我发现如果在将文本设置为新字符串之前将setText:nil设置为nil,则会重置textview,并且电话号码会正常突出显示。 I'm wondering if this is some kind of bug with UITextView in iOS 7.0 我想知道这是否是iOS 7.0中UITextView的某种错误

Either way, this did work for me. 无论哪种方式,这确实对我有用。

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). 当iOS7第一次出现时,这困扰了我,我在这个线程中找到了答案(在设置实际值之前将UITextView的text属性设置为nil)。 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). 然后突然,问题(对我而言,整个字符串被突出显示为链接)被重新裁剪(假设是由于iOS更新)。

What finally did the trick for me was to stop using the text attribute and set the attributedText. 对我来说最终的诀窍是停止使用text属性并设置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. 希望这可以帮助其他人。

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. 我遇到了同样的问题,并且在UITextView上禁用滚动会激活加载时的链接检测,而不是仅在用户与textview交互后才开始工作。 The UITextView also had to be selectable and non-editable. 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. 要检查的另一件事是在单元格的单元格和内容视图上启用了用户交互,否则链接将无法单击。

Check These Lines must be added to use data detector property of textview in UItableView cell. 必须添加“检查这些行”以在UItableView单元格中使用textview数据检测器属性。

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

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 dataDetectorTypes property of UITextView to set what you want to detect in code. 您还可以使用UITextViewdataDetectorTypes属性来设置要在代码中检测的内容。 May just be a storyboard transition problem for you. 可能只是你的故事板过渡问题。

textView.dataDetectorTypes = UIDataDetectorTypeLink;

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 UITextView with `link detection`` 这是一个很好的教程,介绍如何使用`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 GestureRecognizer forwaring touches and enabling/disabling the editing. 诀窍是GestureRecognizer转发触摸并启用/禁用编辑。

You could apply the same thing with the selectable / not selectable issue on iOS7 您可以在iOS7上对selectable / not selectable问题应用相同的iOS7

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. 例如 - 有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). 一个用于单击链接(minimumPressDuration = 0.12),第二个用于放大可编辑模式(minimumPressDuration = 0.5),第三个用于选择(minimumPressDuration = 0.8)。 This solution removes LongPressGestureRecognizer for selecting and second for zooming in editing mode. 此解决方案删除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). 在iOS 9上测试过,但它应该适用于所有版本(iOS 7,8,9)。 I hope it helps! 我希望它有所帮助! :) :)

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;

So using a UITextView keeping it enabled, selectable, not scrollable & links detectable is not as simple as it seems. 因此,使用UITextView保持启用,可选择,不可滚动和链接可检测并不像看起来那么简单。 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). 我在iOS 8中遇到过这种情况。所以我的解决方案是在viewDidLoad中执行类似的操作,然后在textBox编辑完成时将editable属性设置为NO(通常是像doneIsTapped这样的方法)。 The trick here is to set editable property to NO after setting text value to textview is completed. 这里的技巧是在将文本值设置为textview完成后将可编辑属性设置为NO。 This will enable links in the UITextview. 这将启用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. 这使得在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. 希望这有帮助。

在类似的设置中,取消激活UITextViews滚动功能对我来说很有用。

Changing the Tint color to other color actually works. 将色调颜色更改为其他颜色实际上有效。 However if selectable enable the tint will also be the same color. 但是,如果可选择启用,则色调也将是相同的颜色。

使UITextView的滚动属性为否。它将起作用... Self.textView.ScrollingEnable = NO;

This workaround works for me: 此解决方法适用于我:

textView.selectable = YES;
textView.delegate = self;

- (void) textViewDidChangeSelection:(UITextView *)textView;
{
    NSRange range = NSMakeRange(NSNotFound, 0.0);
    if ( range.length && !NSEqualRanges(range, textView.selectedRange) ) {
        textView.selectedRange = range;
    }
}

If you are adding UITextview programmatically just add below lines: 如果您以编程方式添加UITextview,只需添加以下行:

        _textView.userInteractionEnabled = YES;
        _textView.dataDetectorTypes = UIDataDetectorTypeLink;
        _textView.scrollEnabled = NO;
        _textView.editable = NO;

This worked for me. 这对我有用。

None of the above worked for me, instead I did this: 以上都没有为我工作,而是我做了这个:

[self.textView setDataDetectorTypes:UIDataDetectorTypeNone];
[self.textView.setTextColor:[UIColor whiteColor]];
[self.textView setDataDetectorTypes:UIDataDetectorTypeNone];

I did this with my textview that was supposed to detect all types, and which had non detected color set to white. 我用我的textview做了这个,它应该检测所有类型,并且没有检测到颜色设置为白色。 You can change the code to represent your proper color and link types to detect. 您可以更改代码以表示正确的颜色和要检测的链接类型。

While this thread is old, I didn't see an answer that worked for me with Swift, so here goes for Swift 2.2 虽然这个帖子已经老了,但我没有看到一个对Swift有用的答案,所以这里适用于Swift 2.2

textView.dataDetectorTypes = UIDataDetectorTypes.Link
textView.selectable = true

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

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