I'm having a UITabelView
in that I'm having a UILabel
. UILabel
will be populated with different text
contents with one Email id(all mail id's are same). I want to make this Email id clickable. So far I have done is I highlight this email id with blue colour
and underlined it. I add a tap gesture to the UILabel
,but it makes whole UILabel
to be clickable. I want to make this email id is only clickable. Is there is any way to make this possible. I'm having custom table cell class,in that only I added tap gesture.
Asked
Active
Viewed 252 times
1

Jitendra Modi
- 2,344
- 12
- 34

e.k
- 1,333
- 1
- 17
- 36
-
I agree with @BharatModi it will be easier to maintain. – Joshua Dec 27 '16 at 09:40
-
Instead of take label and add gesture on label better way is take button it has clickable functionality and you can manage all the things that you want with label. – Saavaj Dec 27 '16 at 09:42
-
Check the answer [here](http://stackoverflow.com/questions/36110460/how-to-make-text-and-url-link-in-one-uilabel-in-chat-frame) – you may use `UITextView` instead of `UILabel` to make links work as needed. – degapps Dec 27 '16 at 10:12
-
But text view is showing only one line – e.k Dec 27 '16 at 10:46
-
@e.k You must be confusing it with `UITextField`. The `UITextView` is multiline ([see here](https://developer.apple.com/reference/uikit/uitextview)) – degapps Jan 07 '17 at 09:19
3 Answers
1
Use UITextView
instead of UILabel
, And make sure add following, In your CellForRowAtIndexPath
method:
<YourTableViewcell>.textView.editable = NO;
<YourTableViewcell>.textView.dataDetectorTypes = UIDataDetectorTypeAll;
The good thing is you need not handle the Email Click Actions, the UITextView will take care and open an email for you (with clicked an email, prepopulated in TO section).

Imad Ali
- 3,261
- 1
- 25
- 33
-
No, I'm having more than 10 lines of contents. When I use text view means only one line is showing. – e.k Dec 27 '16 at 10:01
-
set [TextView setScrollEnabled:NO] and then adjust height from CGSize sizeThatFitsTextView = [TextView sizeThatFits:CGSizeMake(TextView.frame.size.width, MAXFLOAT)]; will solve your 1 line showing issue. – kaushal Dec 27 '16 at 11:30
1
Use TTAttributeLabel and which will be helpful detecting attributes and much more
Example for link detection :
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
label.enabledTextCheckingTypes = NSTextCheckingTypeLink; // Automatically detect links when the label text is subsequently changed
label.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol)
label.text = @"Fork me on GitHub! (https://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked
NSRange range = [label.text rangeOfString:@"me"];
[label addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range]; // Embedding a custom link in a substring
Following Delegate method is called when a link is detected.
// Delegate methods
- (void)attributedLabel:(TTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url {
// Implement the code
}

Arasuvel
- 2,971
- 1
- 25
- 40
0
in cellForRowAtIndexPath:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"String with a link" attributes:nil];
NSRange linkRange = NSMakeRange(14, 4); // for the word "link" in the string above
NSDictionary *linkAttributes = @{ NSForegroundColorAttributeName : [UIColor colorWithRed:0.05 green:0.4 blue:0.65 alpha:1.0], NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) };
[attributedString setAttributes:linkAttributes range:linkRange];
// Assign attributedText to UILabel
customCell.bodyLabel.attributedText = attributedString;
customCell.bodyLabel.userInteractionEnabled = YES;
[customCell.bodyLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnLabel:)]];
- (void)handleTapOnLabel:(UITapGestureRecognizer *)tapGesture {
UILabel *label = (UILabel *)tapGesture.view;
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeZero];
NSString *string = [label text];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string attributes:nil];
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
// Configure layoutManager and textStorage
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
// Configure textContainer
textContainer.lineFragmentPadding = 0.0;
textContainer.lineBreakMode = label.lineBreakMode;
textContainer.maximumNumberOfLines = label.numberOfLines;
CGPoint locationOfTouchInLabel = [tapGesture locationInView:tapGesture.view];
CGSize labelSize = tapGesture.view.bounds.size;
CGRect textBoundingBox = [layoutManager usedRectForTextContainer:textContainer];
CGPoint textContainerOffset = CGPointMake((labelSize.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x, (labelSize.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y);
CGPoint locationOfTouchInTextContainer = CGPointMake(locationOfTouchInLabel.x - textContainerOffset.x, locationOfTouchInLabel.y - textContainerOffset.y);
NSInteger indexOfCharacter = [layoutManager characterIndexForPoint:locationOfTouchInTextContainer inTextContainer:textContainer fractionOfDistanceBetweenInsertionPoints:nil];
NSRange linkRange = NSMakeRange(14, 4); // it's better to save the range somewhere when it was originally used for marking link in attributed string
if (NSLocationInRange(indexOfCharacter, linkRange)) {
// Open an URL, or handle the tap on the link in any other way
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"your url"]];
}
}