2013-07-24 13 views
5

mógłbym z powodzeniem dodać gesty kranu do części UITextView z następującego kodu:Tap gest ze strony 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; 
} 

pragnę naśladować takie samo zachowanie w UILabel. Problem polega na tym, że UITextInputTokenizer (używane do tokenizacji poszczególnych słów) jest zadeklarowane w UITextInput.h, a tylko UITextView & UITextField jest zgodne z UITextInput.h; UILabel nie. Czy istnieje obejście tego problemu?

+0

cześć przyjacielu, masz sprawdzone zachowanie interakcji użytkownika z UILabel, ponieważ domyślnie userinteraction jest NO z UILabel, trzeba będzie ustawić go YES.Let mi znać to działa albo nie.!!! – NiravPatel

+0

Działanie na całym UILabel nie jest problemem, jest "częścią" UILabel. – n00bProgrammer

+0

Sprawdź http://stackoverflow.com/questions/8811909/getting-the-word-touched-in-a-uilabel-uitextview/21577829#21577829 – TheTiger

Odpowiedz

0

Możesz spróbować https://github.com/mattt/TTTAttributedLabel i dodać link do etykiety. Po naciśnięciu linku otrzymujesz akcję, więc część kliknięcia etykiety działa tylko wtedy, gdy dostosujesz część linku do etykiety. Próbowałem tego w przeszłości i działało bezbłędnie, ale mój klient nie był zainteresowany wykorzystaniem komponentu innej firmy, więc powielał tę funkcjonalność za pomocą UIWebView i HTML.

+0

Ostatecznie rozstrzygnięty na TTTAttributedLabel. Okazuje się, że jest tak ciężki jak UITextView. :( – n00bProgrammer

+1

TTTAttributedLabel ma problemy z wysokością, szczególnie jeśli chodzi o emoji. – Mahouk

-2

To jest podstawowy kod, jak dodać UITapGestureRecognizer do kontroli;

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

Jest to metoda, która nazywamy kiedy stuknął swoją MyLabelName;

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

Doceniam twoją odpowiedź, ale nie potrzebuję gestu na cały etykieta, tylko jej część. – n00bProgrammer

2

Jedną opcją byłoby użyć nieedytowalny UITextView zamiast UILabel. Oczywiście może to być lub nie być odpowiednie rozwiązanie w zależności od Twoich konkretnych potrzeb.

+0

UITextViews są wykluczone. Mogę używać tylko UILabel. – n00bProgrammer

+2

@ n00bProgrammer, możesz sprawić, aby UITextView zachowywał się jak UILabel. – Vignesh

4

Spróbuj tego. Niech twoja etykieta być 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"); 
} 
} 

Kliknięcie w pierwszej połowie etykiety zadziała (Daje wyjście dziennika). Nie druga połowa.

+0

@ n00bProgrammer - Możesz stworzyć własny "CGRect newRect" w oparciu o twoje potrzeby. – iphondroid

2

Oto lekka biblioteka specjalnie dla linków w UILabel FRHyperLabel.

Aby osiągnąć efekt takiego:

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

należy użyć kodu:

//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];