2015-07-21 57 views
-1

我使用UITableview爲人名列表名稱,並執行UISearchbar搜索不同的名稱。我想要更改UITableViewCell用戶搜索特定項目上的textLabel顏色,例如,當我在搜索欄上輸入名稱「Ant」時,需要在UItableview上改變「Ant」的顏色。請幫幫我。如何使用多種顏色在UITableViewCell上製作textLabel?

+0

請張貼一些代碼。 – DHEERAJ

+0

看到此鏈接http://stackoverflow.com/questions/14231879/is-it-possible-to-change-color-of-single-word-in-uitextview-and-uitextfield –

+0

cell.textLabel.textColor = [的UIColor紅色]; –

回答

0

你需要看看使用NSAttributedString指定文本的顏色。您需要處理搜索條件中每次更改時的可見單元格,並檢查何時配置要顯示的單元格,在名稱字符串中找到搜索文本併爲找到的範圍設置適當的格式屬性。

+0

好的謝謝你,但你可以附上一些代碼行樣本 – IKKA

+4

嘗試實施它,並詢問你是否有問題,否則你實際上並沒有真正學到任何東西...... – Wain

0

您可以使用以下方法來滿足您的要求。

- (NSAttributedString *)highlightText:(NSString *)textToBeHighlighted inString:(NSString *)fullString { 
    NSDictionary *attributeForFullText = @{ 
            NSForegroundColorAttributeName : [UIColor blackColor], 
            NSFontAttributeName    : [UIFont systemFontOfSize:10.0f] 
            // And more...... 
            }; 
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullString attributes:attributeForFullText]; 


    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[fullString rangeOfString:textToBeHighlighted]]; 

    /* you can use the above statement to do single style, 
    but if you want to do something more like size, font, alignment etc. 
    then you can do by below */ 

    [attributedString addAttributes:dictionary_with_more_style range:[fullString rangeOfString:textToBeHighlighted]]; 
    return attributedString; 
} 

而只是把它從你的cellForRowAtIndexPath

UILabel *titleLabel; 
NSString *fullTitle; 
NSString *searchString; 

[titleLabel setAttributedText:[self highlightText:searchString inString:fullTitle]; 
0

您可以使用UILabelattributedText財產。這將裝飾標籤上的文字。對於UITableViewCell創建類別和寫入的方法並執行以下操作:

@implementation UITableViewCell(asdf) 

- (void)setText:(NSString *)text withMatchingText:(NSString *)matchingText; 
{ 
    // Set the default text color to the label text. 
    self.textLabel.textColor = // Default text color 
    if (matchingText.length > 0) { 

     // Create the color for the matching text. 
     UIColor *matchingColor = // Text color for the matching text, eg. "Ant" 

     //Find the range of the matching text from the whole text. 
     NSRange firstMatchingRange = [text rangeOfString:matchingText options:NSCaseInsensitiveSearch]; 

     // Create an attributed string with matching color for your matching text on the whote text. 
     NSMutableAttributedString *mString = [[NSMutableAttributedString alloc] initWithString:text]; 
     [mString addAttribute:NSForegroundColorAttributeName 
         value:matchingColor 
         range:firstMatchingRange]; 
     self.textLabel.attributedText = mString; 

    }else { 
     self.textLabel.text = text; 
    } 
} 

在你的viewController的tableView:cellForRowAtIndexPath:方法執行以下操作:

NSString *text = // the name property 
cell setText:text withMatchingText:self.searchBar.text]; 

self.searchBar是類型的屬性UISearchBar你有你的ViewController連接在故事板/ xib上的搜索欄。

上面的代碼確實喜歡,如果您的搜索欄有文本,那麼它將matchingColor設置爲匹配文本(例如Ant),另一個字母(ony)將是默認顏色。如果沒有更多的匹配文本,那麼整個文本將以默認文本顏色顯示。

這可能對你有幫助。

0

檢查此代碼。它可以解決你的問題。 將此代碼放入控制器的tableView:cellForRowAtIndexPath:方法中。

NSString *title = @"Test this answer"; 
NSString *searchText = @"ans"; 

NSMutableAttributedString *originalString = [[NSMutableAttributedString alloc] initWithString:title]; 

NSRange range = [title rangeOfString:searchText]; 

if (range.location == NSNotFound) { 
    NSLog(@"No match found"); 
} 
else { 
    NSLog(@"Found the range of the substring at (%lu, %lu)", (unsigned long)range.location, range.location + range.length); 
    [originalString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range]; 
} 

cell.lbl_Title.attributedText = originalString;