簡體   English   中英

在UITableViewCell中處理NSRange

[英]Handling NSRange In UITableViewCell

我在NSRange遇到了一些問題。 我有一個CommentViewController可以正常工作,但是我想擁有一個tapGesture並在@之后更改文本的顏色,就像Twitter上提到的那樣。 由於某些原因,在某些單元格中,所有文本都會更改顏色,而不僅僅是提及。 這是代碼:

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

我在做什么錯,我該如何在彩色部分添加手勢?

更換

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

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

分隔單詞而不是字母時,應使用空格分隔組成部分!!

你有試過嗎

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;
NSString *message = @"hello@gmail.com";

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

在此處輸入圖片說明

這相當粗糙,建議您添加一些條件檢查,以確保不會因崩潰而崩潰。 @之前,但確實可以。

請嘗試這個

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

我想通了。 在大多數情況下,我認為這是正則表達式的問題! 這是我的最終代碼:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM