1

Ich verwende NSAttributed String, die mehrere Farben enthält. Bitte überprüfen Sie dieses Bild. enter image description hereGet NSAttributed Zeichenfolge Farbe beim Tippen (Wort Farbe erkennen)

ich ein Wort bin Auswahl mit TapGesture ... Für Beispiel- i „red“

- (void)tapGestureRecognizerHandle:(UITapGestureRecognizer *)tapGestureRecognizer { 

SETextView *textView = self.textV; 

CGPoint location = [tapGestureRecognizer locationInView: self.textV]; 
NSLog(@"Tap Gesture Coordinates: %.2f %.2f -- %@", location.x, location.y,textView.text); 
CGPoint position = CGPointMake(location.x, location.y); 
//get location in text from textposition at point 
UITextPosition *tapPosition = [textView closestPositionToPoint:position]; 


UITextRange *textRange = [textView.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight]; 
NSString *tappedSentence; 
if (textRange != nil) 
{ 
    tappedSentence = [textView textInRange:textRange]; 


} 

    else 
{ 
    tappedSentence = textView.text; 
} 
} 

Jetzt wollen die Farbe des gewählten Wortes wissen, ich habe ausgewählt irgendeine eine Idee haben, bitte teilen

Dank

+0

Sie sind Verwendung dieses Label helfen: https://github.com/TTTAttributedLabel/TTTAttributedLabel –

+0

keine Kirit ... ich bin mit einfachen Textview mit Tapgesture –

+0

@VarinderSingh SETextView ist Ihre eigene Klasse oder ist es aus der Bibliothek? –

Antwort

0

gefunden antwort. Hoffnung dieser Jemand

textView.selectedTextRange=textRange; 

NSRange oldRange = [self selectedRangeInTextView:textView]; 

NSAttributedString *selectedString = [textView.attributedText attributedSubstringFromRange:oldRange]; 
[selectedString enumerateAttributesInRange:NSMakeRange(0, [selectedString length]) 
            options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired 
           usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) { 
            UIColor *StringColor = [attributes objectForKey:NSForegroundColorAttributeName]; 


            self.backgroundColor=StringColor; 

            if ([StringColor isEqual:[UIColor blueColor]]) { 
             NSLog(@"blue Color"); 

            } else { 
             NSLog(@"another Color"); 

            } 
      }]; 
+1

Tipp: Verwenden Sie 'enumerateAttribute: inRange: Optionen: usingBlock:' statt nur nach 'NSForegroundColorAttributeName', macht es die Code klarer auf Ihr Ziel. Sie sollten auch prüfen, ob 'stringColor' gleich Null ist oder nicht. – Larme