6

Ho un NSAttributedString in questo modo:Aggiungere un gesto rubinetto per una parte di un UILabel

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

Il NSMutableAttributedString viene impostato su un UILabel in questo modo:

label.attributedText = str; 

Come faccio a fare un rubinetto gesto (o qualcosa cliccabile) su un altro viewcontroller per le parole '@clickhere nella stringa sopra?

Grazie!

risposta

5

credo, il modo migliore è l'aggiunta del UIGestureRecognizer al UILabel e convalidare il cornice che ti piacerebbe.

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

Semplicemente prima aggiungere un gesto all'etichetta

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

controllo la vostra zona gesto nel metodo seguito

- (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

vorrei solo aggiungere alla risposta di Ramshad, su come affrontare con il telaio valido.

Per questo è possibile prendere in considerazione l'utilizzo di UITextView anziché UILabel, che non consente di accedere a come gestisce il layout del testo. Disabilitando la modifica, la selezione e lo scorrimento, UITextView si comporta all'incirca come una UILabel, ad eccezione di alcuni padding che è necessario rimuovere.

Per comodità, è possibile aggiungere una piccola categoria a UITextView, in cui si scrive un metodo per verificare se un punto tocca uno dei caratteri nell'intervallo.

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

Questo potrebbe funzionare anche per un frammento di testo contenente più parole scaglionati su più righe dovute a capo automatico. Funzionerebbe anche su testi localizzati mentre trattiamo i glifi stampati.

+0

Ciao Li, hai testato questo codice? – Zeb

Problemi correlati