Tap Gesture sur une partie de UILabel

Je pourrais append avec succès des gestes tapés à une partie de UITextView avec le code suivant:

UITextPosition *pos = textView.endOfDocument;// textView ~ UITextView for (int i=0;i<words*2-1;i++){// *2 since UITextGranularityWord considers a whitespace to be a word UITextPosition *pos2 = [textView.tokenizer positionFromPosition:pos toBoundary:UITextGranularityWord inDirection:UITextLayoutDirectionLeft]; UITextRange *range = [textView textRangeFromPosition:pos toPosition:pos2]; CGRect resultFrame = [textView firstRectForRange:(UITextRange *)range ]; UIView* tapViewOnText = [[UIView alloc] initWithFrame:resultFrame]; [tapViewOnText addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(targetRoutine)]]; tapViewOnText.tag = 125; [textView addSubview:tapViewOnText]; pos=pos2; } 

Je souhaite imiter le même comportement dans un UILabel . Le problème est, UITextInputTokenizer (utilisé pour marquer les mots individuels) est déclaré dans UITextInput.h , et seulement UITextView & UITextField conforment à UITextInput.h ; UILabel ne le fait pas. Y at-il une solution de contournement pour cela?

Vous pouvez essayer https://github.com/mattt/TTTAtsortingbutedLabel et append un lien vers l'label. Lorsque le lien est pressé, vous obtenez une action, donc une partie du clic sur l'label ne fonctionne que si vous devez personnaliser la partie lien de l'label. J'ai essayé ceci dans le passé et cela a fonctionné parfaitement mais mon client n'était pas intéressé par l'utilisation d'un composant tiers donc dupliqué cette fonctionnalité en utilisant UIWebView et HTML.

Essaye ça. Laissez votre label être label :

  //add gesture recognizer to label UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] init]; [label addGestureRecognizer:singleTap]; //setting a text initially to the label [label setText:@"hello world i love iphone"]; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint touchPoint = [touch locationInView:self.view]; CGRect rect = label.frame; CGRect newRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width/2, rect.size.height); if (CGRectContainsPoint(newRect, touchPoint)) { NSLog(@"Hello world"); } } 

Cliquer sur la première moitié de l'label fonctionnera (cela donne une sortie de journal). Pas l'autre moitié.

Une option serait d'utiliser un UITextView non éditable au lieu d'un UILabel. Bien sûr, cela peut ou non être une solution appropriée en fonction de vos besoins exacts.

Voici une bibliothèque allégée spécialement pour les liens dans UILabel FRHyperLabel .

Pour get un effet comme celui-ci:

Lorem ipsum dolor assis amet, consectetur adipiscing elit. Pellentesque quis blandit eros, assis amet vehicula justo. Nam à urna neque. Mécène ac sem eu porta dictum nec vel tellus.

utiliser le code:

 //Step 1: Define a normal atsortingbuted ssortingng for non-link texts NSSsortingng *ssortingng = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque quis blandit eros, sit amet vehicula justo. Nam at urna neque. Maecenas ac sem eu sem porta dictum nec vel tellus."; NSDictionary *atsortingbutes = @{NSFontAtsortingbuteName: [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]}; label.atsortingbutedText = [[NSAtsortingbutedSsortingng alloc]initWithSsortingng:ssortingng atsortingbutes:atsortingbutes]; //Step 2: Define a selection handler block void(^handler)(FRHyperLabel *label, NSSsortingng *subssortingng) = ^(FRHyperLabel *label, NSSsortingng *subssortingng){ NSLog(@"Selected: %@", subssortingng); }; //Step 3: Add link subssortingngs [label setLinksForSubssortingngs:@[@"Lorem", @"Pellentesque", @"blandit", @"Maecenas"] withLinkHandler:handler]; 

C'est un code de base pour savoir comment append UITapGestureRecognizer à votre contrôle;

 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; [MyLabelName addGestureRecognizer:singleTap]; [self.view addSubView:MyLabelName] 

C'est la méthode qui appelle quand vous avez tapé votre MyLabelName ;

 -(void)handleSingleTap:(UILabel *)myLabel { // do your stuff; }