2016-12-27 10 views
1

私はUITabelViewを持っています。私はUILabelを持っています。 UILabelにはtextの内容とのメールID(すべてのメールIDは同じです)が入力されます。このメールIDをクリック可能にしたい今まで私が行ったのは、blue colourの下にと書かれています。 UILabelにタップジェスチャーを追加しますが、UILabelとなります。です。私はこの電子メールIDをクリック可能にしたいと思っています。これを可能にする方法はありますか?私はカスタムテーブルセルクラスを持っていますが、それだけでタップジェスチャーを追加しました。UITableViewのUiLabelで特定の場所をクリックする方法は?

+0

私はそれを維持するために容易になります@BharatModiに同意します。 – Joshua

+0

ラベルを取ってラベルにジェスチャーを追加するのではなく、取るボタンがクリック可能な機能を備えていて、ラベルで必要なものすべてを管理できます。 – Saavaj

+0

答えを確認する[ここ](http://stackoverflow.com/questions/36110460/how-to-make-text-and-url-link-in-one-uilabel-in-chat-frame) - あなたが使うことができるUILabelの代わりに 'UITextView'を使用して、必要に応じてリンクを動作させます。 – degapps

答えて

1

利用UITextView代わりのUILabel、そして追加は、以下のことを確認しますが、あなたのCellForRowAtIndexPath方法で:

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

良いことは、あなたがメールをクリックアクションを処理する必要はありませんで、UITextViewは、世話をし、電子メールを開きます。 (あなたは電子メールをクリックして、TOセクションにあらかじめ入力しています)。

+0

いいえ、私は10行以上の内容を持っています。テキストビューを使用すると、1行だけが表示されます。 –

+0

[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 

が呼び出されます。 cellForRowAtIndexPathで

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

申し訳ありませんが、私は外部ライブラリを使用したくありません。 –

+0

@ e.k次に、dataDetectortypesでUITextViewを使用します。 – Arasuvel

0

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"]]; 
    } 
} 
関連する問題