2013-07-24 9 views
5

potrei aggiungere con successo gesti tap ad una parte della UITextView con il seguente codice:Tap gesto da parte di UILabel

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; 
} 

desidero di imitare lo stesso comportamento in una UILabel. Il problema è, UITextInputTokenizer (utilizzato per tokenizzare le singole parole) è dichiarato in UITextInput.h e solo UITextView & UITextField conforme a UITextInput.h; UILabel no. C'è una soluzione per questo?

+0

amico Ciao, hai controllato il comportamento di interazione con l'utente UILabel, perché per UserInteraction default è no di UILabel, si dovrà impostare YES.Let essere informato sta funzionando o no.!!! – NiravPatel

+0

L'azione su un'intera UILabel non è un problema, è "parte" di UILabel. – n00bProgrammer

+0

Si prega di controllare http://stackoverflow.com/questions/8811909/getting-the-word-touched-in-a-uilabel-uitextview/21577829#21577829 – TheTiger

risposta

0

Si potrebbe provare https://github.com/mattt/TTTAttributedLabel e aggiungere un collegamento all'etichetta. Quando si preme il collegamento si ottiene un'azione, quindi parte del clic dell'etichetta funziona solo come necessario per personalizzare la parte di collegamento dell'etichetta. L'ho provato in passato e funzionava perfettamente, ma il mio cliente non era interessato all'utilizzo di un componente di terze parti, quindi ho duplicato questa funzionalità utilizzando UIWebView e HTML.

+0

Definito infine su TTTAttribuitoLabel. Risulta pesante quanto un UITextView. :( – n00bProgrammer

+1

TTTAttributoLabel ha problemi di altezza, specialmente quando si parla di emoji. – Mahouk

-2

Questo è il codice di base per come è possibile aggiungere UITapGestureRecognizer al proprio controllo;

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

Questo è il metodo che la chiamata quando si è toccato il vostro MyLabelName;

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

Apprezzo la tua risposta, ma non ho bisogno di un gesto sull'intero etichetta, solo una parte. – n00bProgrammer

2

Un'opzione consisterebbe nell'utilizzare un UITextView non modificabile anziché un UILabel. Ovviamente questa può o meno essere una soluzione adatta a seconda delle vostre esigenze.

+0

UITextViews sono fuori questione. Posso usare solo UILabels. – n00bProgrammer

+2

@ n00bProgrammer, è possibile far funzionare UITextView come UILabel. – Vignesh

4

Prova questo. Lasciate che il vostro marchio sia 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"); 
} 
} 

Cliccando sul primo semestre dell'etichetta funzionerà (Dà output del log). Non l'altra metà.

+0

@ n00bProgrammer - È possibile creare il proprio 'CGRect newRect' in base alle proprie esigenze. – iphondroid

2

Questa è una libreria leggera per collegamenti in UILabel FRHyperLabel.

per ottenere un effetto simile a questo:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque quis blandit eros, sit amet vehicula justo. Nam a urna neque. Maecenas ac sem eu sem porta dictum nec vel tellus.

utilizzare il codice:

//Step 1: Define a normal attributed string for non-link texts 
NSString *string = @"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 *attributes = @{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]}; 

label.attributedText = [[NSAttributedString alloc]initWithString:string attributes:attributes]; 


//Step 2: Define a selection handler block 
void(^handler)(FRHyperLabel *label, NSString *substring) = ^(FRHyperLabel *label, NSString *substring){ 
    NSLog(@"Selected: %@", substring); 
}; 


//Step 3: Add link substrings 
[label setLinksForSubstrings:@[@"Lorem", @"Pellentesque", @"blandit", @"Maecenas"] withLinkHandler:handler]; 
Problemi correlati