2016-12-27 80 views
1

我有一個UITabelView因爲我有一個UILabelUILabel將填充不同的text內容與一個電子郵件ID(所有郵件ID都相同)。我想讓這個電子郵件ID可點擊。到目前爲止我所做的是我突出這個電子郵件ID與blue colour下劃線它。我在UILabel上添加了一個輕擊手勢,但它會使整個UILabel可點擊。我想讓這個電子郵件ID只能點擊。有沒有什麼辦法可以做到這一點。我有自定義表格單元類,只有我加點擊手勢如何使特定位置在UITableView中的UiLabel中可點擊?

+0

我@BharatModi同意它會更容易維護。 – Joshua

+0

取代標籤並在標籤上添加手勢更好的方法是採用按鈕,它具有可點擊的功能,您可以使用標籤管理所有您想要的內容。 – Saavaj

+0

檢查答案[here](http://stackoverflow.com/questions/36110460/how-to-make-text-and-url-link-in-one-uilabel-in-chat-frame) - 你可以使用'UITextView'而不是'UILabel'來根據需要建立鏈接。 – degapps

回答

1

使用UITextView,而不是UILabel,並確保添加以下,在你CellForRowAtIndexPath方法:

<YourTableViewcell>.textView.editable = NO; 
<YourTableViewcell>.textView.dataDetectorTypes = UIDataDetectorTypeAll; 

的好處是你不用處理電子郵件點擊操作,則UITextView的會照顧並打開電子郵件爲你(點擊一個電子郵件,預填充到部分)。

+0

不,我有超過10行的內容。當我使用文本視圖意味着只有一行顯示。 –

+0

set [TextView setScrollEnabled:NO],然後從CGSize調整高度sizeThatFitsTextView = [TextView sizeThatFits:CGSizeMake(TextView.frame.size.width,MAXFLOAT)];將解決你的1行顯示問題。 – kaushal

1

使用TTAttributeLabel和這將是對鏈路檢測有用的檢測的屬性和更

實施例:檢測到鏈路時

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 

以下委派方法被調用。

// Delegate methods 
- (void)attributedLabel:(TTTAttributedLabel *)label 
    didSelectLinkWithURL:(NSURL *)url { 
// Implement the code 
} 
+0

對不起,我不想使用外部庫。 –

+0

@ e.k然後使用帶有dataDetectortypes的UITextView。 – Arasuvel

0

中的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"]]; 
    } 
} 
相關問題