Change the background color of a substring of a drawn NSString?
I am using the ABTableViewCell subclass by Atebits to optimize scrolling for rather large potential data sources depending on what my search returns. (Either way though, I'd highly recommend the subclass for even the simplest tableviews, they scroll like butter!)
I have an NSString drawn in a view:
[nameLabel drawInRect:nameRect withFont:nameLabelFont lineBreakMode:UILineBreakModeWordWrap];
I thought it would be a cool feature that the searched word (which is "Name CONTAINS searchText") be highlighted. Is this as simple as changing the background color behind a substring of nameLabel?
EDITS:
NSAttributedString seems to be what I'm looking for, but turns out it doesn't 开发者_开发问答exist for the iPhone.
From further reading, it would seem that HTML and a UIWebView is the way to go for this... but that seems awfully expensive. Is this really the solution?
Hey, check out TTStyledLabel - it might do what you want.
How about using an NSAttributedString? http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/Reference/Reference.html
Wild idea - is your text a single line? If so you could compute the width of "Name " and then the width of "CONTAINS" and so you would know where exactly it's drawn on screen. Once you do simply drop a semi-transparent colored UIView on top of the text.
I didn't try it myself, but that's what I would do if I had to make it work.
精彩评论