2016-07-11 12 views
0

Ich habe ein kleines Problem mit NSRange. Ich habe eine CommentViewController, die funktioniert, aber ich versuche, eine tapGesture haben und ändern Sie die Farbe des Textes direkt nach der @, wie eine Twitter-Erwähnung. Aus irgendeinem Grund ändert in einigen Zellen der gesamte Text die Farbe, anstatt nur die Erwähnung. Hier ist der Code:Behandlung von NSRange in UITableViewCell

NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:message[@"text"]]; 

NSArray *words=[message[@"text"] componentsSeparatedByString:@""]; 

for (NSString *word in words) { 
    if ([word hasPrefix:@"@"]) { 
      NSRange range=[message[@"text"] rangeOfString:word]; 
      [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range]; 
    } 
} 
[cell.bodyLabel setAttributedText:string]; 

Was mache ich falsch und wie füge ich eine Geste zum farbigen Teil?

+0

Sie möchten die Textfarbe ändern. ich meine ex: iiiii @ gmail Sie können die Google Mail-Farbe ändern. –

+0

Ich möchte "@gmail" ändern - ja! – HalesEnchanted

+0

NSArray * words = [Nachricht [@ "Text"] componentsSeparatedByString: @ ""]; zu NSArray * words = [Nachricht [@ "Text"] componentsSeparatedByString: @ ""]; Raum fehlt dort –

Antwort

0

Hast Du diesen

NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:message[@"text"]]; 

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(@[A-Za-z0-9]*)" options:kNilOptions error:nil]; 

NSRange range = NSMakeRange(0,message[@"text"].length); 

[regex enumerateMatchesInString:message[@"text"] options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { 

    NSRange subStringRange = [result rangeAtIndex:1]; 
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:subStringRange]; 
}]; 

cell.bodyLabel.attributedText = attributedString; 
+0

Vielen Dank für Ihre Hilfe. Wenn strFirst zum Beispiel mit @ gmail startet, ist der gesamte Text immer noch rot. Wenn ich einen Text davor lege: "haha @gmail" - es funktioniert. Was könnte es sein? – HalesEnchanted

+0

Kannst du den ganzen Eingang setzen, damit ich verstehen kann. wenn '@ gmail' bedeutet, wenn Platz vorhanden ist, dann willst du auch gmail mit rot kommen? willst du das? –

+0

Ich habe den Code wie du :) Das Problem ist, wenn der @ beginnt den Satz, es funktioniert nicht. Hier ist ein Foto: http://i.imgur.com/URTgMIJ.png – HalesEnchanted

0

ersetzen

NSArray *words=[message[@"text"] componentsSeparatedByString:@""]; 

Mit

NSArray *words=[message[@"text"] componentsSeparatedByString:@" "]; 

Sie sollten Komponente durch Leerzeichen getrennt, wie Sie Wörter die Buchstaben nicht zu trennen !!

0
NSString *message = @"[email protected]"; 

NSRange initialRange = [message rangeOfString:@"@"]; 
NSRange endRange = [message rangeOfString:@"."]; 
NSUInteger length = endRange.location - initialRange.location; 
NSRange markupRange = NSMakeRange(initialRange.location, length); 
NSMutableAttributedString * attString = [[NSMutableAttributedString alloc]initWithString:message]; 
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:markupRange]; 

[cell.bodyLabel setAttributedText:attString]; 

versuchen enter image description here

Das ist ziemlich rau, und ich würde empfehlen, einige bedingte Kontrollen Hinzufügen Sie Abstürze für Dinge wie die nicht bekommen, um sicherzustellen, dass . vor dem @, aber es funktioniert.

0

plz versuchen diese

NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:message[@"text"]]; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(@[A-Za-z0-9]*)" options:kNilOptions error:nil]; 
NSArray *a = [message[@"text"]componentsSeparatedByString:@" "]; 

for (NSString *str in a) 
{ 
    NSRange range = NSMakeRange(0,str.length); 

    [regex enumerateMatchesInString:str options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { 

     NSRange subStringRange = [result rangeAtIndex:1]; 
     [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:subStringRange]; 
    }]; 
    label.attributedText = attributedString; 
} 
+0

Das macht alles rot! Woher? – HalesEnchanted

0

ich das herausgefunden. Zum größten Teil glaube ich, dass es ein Problem mit der Regex war! Hier ist mein endgültiger Code:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:strFirst]; 
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:(NSRange){0, strFirst.length}]; 
UIColor *mentionColor = [UIColor hx_colorWithHexRGBAString:@"2cb8f6"]; 
NSError *error = nil; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[#@](\\w+)" options:0 error:&error]; 
NSArray *matches = [regex matchesInString:strFirst options:0 range:NSMakeRange(0, strFirst.length)]; 
for (NSTextCheckingResult *match in matches) { 
    NSRange wordRange = [match rangeAtIndex:0]; 
    // NSString *word = [strFirst substringWithRange:wordRange]; 
    [attributedString addAttribute:NSForegroundColorAttributeName 
          value: mentionColor 
          range:wordRange]; 
} 
cell.bodyLabel.attributedText = attributedString;