我使用UITableview
爲人名列表名稱,並執行UISearchbar
搜索不同的名稱。我想要更改UITableViewCell
用戶搜索特定項目上的textLabel顏色,例如,當我在搜索欄上輸入名稱「Ant」時,需要在UItableview
上改變「Ant」的顏色。請幫幫我。如何使用多種顏色在UITableViewCell上製作textLabel?
-1
A
回答
0
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
您可以使用UILabel
attributedText
財產。這將裝飾標籤上的文字。對於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;
相關問題
- 1. UITableViewCell textLabel顏色不變
- 2. UITableViewCell中的多種顏色
- 3. UItableviewcell textlabel背景顏色問題
- 4. iphone uitableviewcell - 多個顏色在同一個單元格textLabel?
- 5. 如何製作帶有2種不同顏色標籤的UITableViewCell?
- 6. 如何使用幾種顏色繪製
- 7. 多種顏色
- 8. 如何在java.awt.graphics中使用drawString()時使用多種顏色?
- 9. UITableView單元格textLabel顏色
- 10. 如何製作多種顏色的單個UILabel
- 11. 如何製作多種顏色的指示燈
- 12. UITableViewCell textLabel cut
- 13. 使用matplotlib使用多種顏色的顏色軸脊柱
- 14. 更改UITableViewCell textLabel背景色以清除
- 15. 如何在選擇UITableViewCell時更改textLabel?
- 16. 在屏幕上出現多種顏色
- 17. 如何使用多種顏色在一個屏幕上使用批處理
- 18. TTTAttributedLabel多種顏色
- 19. 如何在標籤中使用多種顏色(Xcode/iPad)
- 20. 如何在map.plot中使用多種顏色python的kwargs
- 21. 如何在java中使用多種顏色的文本?
- 22. 如何在richtextbox中使用多種顏色
- 23. 如何繪製R中多種顏色的多義線?
- 24. 如何在空UITableViewCell中設置顏色?
- 25. matplotlib繪圖中使用多種顏色
- 26. 使用多種文字顏色[Html.fromhtml()]
- 27. 如何製作新顏色?
- 28. 畫布上用多種顏色繪製線條
- 29. 在畫布上繪製的多種顏色
- 30. 繪製多種顏色的路徑
請張貼一些代碼。 – DHEERAJ
看到此鏈接http://stackoverflow.com/questions/14231879/is-it-possible-to-change-color-of-single-word-in-uitextview-and-uitextfield –
cell.textLabel.textColor = [的UIColor紅色]; –