6

Mam NSAttributedString tak:Dodaj gest kranu do części UILabel

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"testing it out @clickhere"]; 
NSInteger length = str.length; 
[str addAttribute:NSForegroundColorAttributeName value:[UIColor bestTextColor] range:NSMakeRange(0,length)]; 

NSMutableAttributedString zostanie ustawiony na UILabel tak:

label.attributedText = str; 

Jak zrobić podsłuch gest (lub coś klikalnego) do innego kontrolera view dla słowa "@kliknij w powyższym łańcuchu?

Dzięki!

Odpowiedz

5

myślę, że najlepszym sposobem jest dodanie UIGestureRecognizer do UILabel i zatwierdzanie rama, którą chcesz.

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; 
[_yourLabel addGestureRecognizer:singleTap]; 

- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer 
{ 
    CGPoint touchPoint = [tapRecognizer locationInView: _yourLabel]; 

    //Modify the validFrame that you would like to enable the touch 
    //or get the frame from _yourLabel using the NSMutableAttributedString, if possible 
    CGRect validFrame = CGRectMake(0, 0, 300, 44); 

    if(YES == CGRectContainsPoint(validFrame, touchPoint) 
    { 
     //Handle here. 
    } 
} 
1

Wystarczy najpierw dodać gest do etykiety

[label setUserInteractionEnabled:YES]; 
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; 
[label addGestureRecognizer:gesture]; 

kontrolować obszar gest w poniższej metody

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer 
{ 
    static CGRect touchableRect = CGRectMake(100.0f, 0.0f, 100.0f, 50.0f); // Give your rect as you need. 
    CGPoint touchPoint = [gestureRecognizer locationInView:self.view]; 
    if (CGRectContainsPoint(touchableRect, touchPoint)) 
    { 
     //User has tap on where you want. Do your other stuff here 
    } 
} 
0
UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)]; 
Tap.numberOfTapsRequired = 1; 
// label Name Is your Label Name 
[labelName addGestureRecognizer:Tap]; 
-(void)tapDetected 
    { 
    //your code 
    } 
0

Chciałbym tylko dodać na odpowiedź Ramshad, w jaki sposób radzić sobie z poprawnym ramce.

W tym celu można rozważyć użycie UITextView zamiast UILabel, co nie daje dostępu do sposobu zarządzania układem tekstu. Po wyłączeniu edycji, wyboru i przewijania UITextView zachowuje się mniej więcej tak samo jak UILabel, z wyjątkiem niektórych dopełnień, które należy usunąć.

Dla wygody możesz dodać niewielką kategorię do UITextView, w której piszesz metodę sprawdzania, czy punkt dotyka dowolnego znaku w zakresie.

- (BOOL)point:(CGPoint)point touchesSomeCharacterInRange:(NSRange)range 
{ 
    NSRange glyphRange = [self.layoutManager glyphRangeForCharacterRange:range actualCharacterRange:NULL]; 

    BOOL touches = NO; 
    for (NSUInteger index = glyphRange.location; index < glyphRange.location + glyphRange.length; index++) { 
     CGRect rectForGlyphInContainer = [self.layoutManager boundingRectForGlyphRange:NSMakeRange(index, 1) inTextContainer:self.textContainer]; 
     CGRect rectForGlyphInTextView = CGRectOffset(rectForGlyphInContainer, self.textContainerInset.left, self.textContainerInset.top); 

     if (CGRectContainsPoint(rectForGlyphInTextView, point)) { 
      touches = YES; 
      break; 
     } 
    } 

    return touches; 
} 

Będzie to również działać w przypadku fragmentu tekstu zawierającego wiele wyrazów rozprzestrzeniających się w wielu wierszach z powodu zawijania wyrazów. Będzie również działać na zlokalizowanych tekstach, ponieważ mamy do czynienia z drukowanymi glifami.

+0

Witaj Li, czy przetestowałeś ten kod? – Zeb