Uitextview Link Detection in iOS 7

UITextView link detection in iOS 7

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

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.

UITextViews in a UITableView link detection bug in iOS 7

This appears to be a bug in iOS 7.0's UITextViews. A similar question has a workaround which seems to help: set the text view's text to nil before setting it to the new text string.

iOS 7 UITextView link detection crash in UITableView

The crash happens when two cells with data type are being dequeued while
using the same cell identifier.
It seems to be a bug in iOS, but Apple may have good reasons to implement it this way.
(memory wise)

And so the only 100% bullet proof solution is to provide a unique identifier for cells
containing data types.
This doesn't mean you will set a unique identifier to all cells in your table, of course,
as it will eat up too much memory and your table scroll will be really slow.

You can use NSDataDetector to determine if a matched type was found on your text,
and only then save the found object as the cell identifier, like so:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

NSString *row = [self.dataSource objectAtIndex:indexPath.row];
static NSDataDetector *detector = nil;
if (!detector)
{
NSError *error = NULL;
detector = [[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeLink | NSTextCheckingTypePhoneNumber error:&error];
}

NSTextCheckingResult *firstDataType = [detector firstMatchInString:row
options:0
range:NSMakeRange(0, [row length])];
NSString *dataTypeIdentifier = @"0";
if (firstDataType)
{
if (firstDataType.resultType == NSTextCheckingTypeLink)
dataTypeIdentifier = [(NSURL *)[firstDataType URL] absoluteString];
else if (firstDataType.resultType == NSTextCheckingTypePhoneNumber)
dataTypeIdentifier = [firstDataType phoneNumber];
}

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell_%@", dataTypeIdentifier];

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
...

Note: Initializing NSDataDetector *detector as static
rather than initialize it for each cell improves performance.

UITextView should detect links, but otherwise, should propagate touch to view below

Instead of preventing your text view from becoming first responder, you need to subclass the hitTest method so that it returns the textView when the click occured inside a link and return nil otherwise.

@interface LinkOnlyTextView : UITextView
@end

@implementation LinkOnlyTextView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
NSUInteger glyphIndex = [self.layoutManager glyphIndexForPoint:point inTextContainer:self.textContainer fractionOfDistanceThroughGlyph:nullptr];
NSUInteger characterIndex = [self.layoutManager characterIndexForGlyphAtIndex:glyphIndex];
if (characterIndex < self.textStorage.length) {
if ([self.textStorage attribute:NSLinkAttributeName atIndex:characterIndex effectiveRange:nullptr]) {
return self;
}
}
return nil;
}

@end

There is a Swift version of this code snippet provided by @blwinters in a later answer to this question here: https://stackoverflow.com/a/47913329/1279096

Link detection in a UITextView inside a UITableViewCell - iOS 7

try again after increase row height of a cell in [tableView:heightForRowAtIndexPath].

If it works, you need to change of height of cell dynamically.

UITextView Not Detecting Links

Set your Textview behavior to Selectable ,

Sample Image

And make sure you have enabled User Interaction .

Sample Image

How to make UITextView detect links for website, mail and phone number

If you are using OS3.0

you can do it like the following

textview.editable = NO;
textview.dataDetectorTypes = UIDataDetectorTypeAll;


Related Topics



Leave a reply



Submit