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 UITextView
s. 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 ,
And make sure you have enabled User Interaction .
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
Dateformatter Returns Unexpected Date for Timezone
Upload Files to Dropbox from iOS App with Swift
Integration New Facebook Sdk by Swift
Maskstobounds VS. Clipstobounds
Format Uitextfield Text Without Having Cursor Move to the End
Navigation Bar Under Status Bar After Video Playback in Landscape Mode
Writing an iOS 8 Share Extension Without a Storyboard
Scrolling Slow on Mobile/iOS When Using Overflow:Scroll
iPhone iOS Will Not Display Box-Shadow Properly
iOS Safari Memory Usage with "-Webkit-Transform"
How to Do Programmatically Gradient Border Color Uibutton with Swift
How to Combine Two Dictionary Instances in Swift
How to Make a Function Execute Every Second in Swift
Presenting a View Controller Modally from an Action Sheet's Delegate in iOS8 - iOS11
How to Enable Arc Project-Wide in Xcode 4.2
How to Set Cmake_C_Compiler and Cmake_Cxx_Compiler for Building Assimp for iOS
Using Core Data, Icloud and Cloudkit for Syncing and Backup and How It Works Together