tags:

views:

327

answers:

3

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?

A: 

How about using an NSAttributedString? http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/Reference/Reference.html

darren
Except NSAttributedString is not available on the phone: http://stackoverflow.com/questions/729135/why-no-nsattributedstring-on-the-iphone
Matt Long
oh good point, i didn't realize it wasn't available on iPhone OS yet.
darren
A: 

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.

DenNukem
I wish it was that simple. My text is very variable in size. I actually have to compute the height of each cell because of it.
Mike A
Hm. I don't see what else can you do aside from doing manual wordwrap: you could do a binary search comparing pixel length of a substring with the boundaries of the box for different character length of the substring. After you do that you will have an array of one-line strings of text, and then you can go back to my first proposed solution.It's a real pain to implement, but it will give you an edge over everyone else.At any rate I would wait until Apple's event in a few days - if the OS 4.0 comes out soon they might have a solution to your problem there.
DenNukem
And as I expected: NSAttributedString is added to SDK 3.2 announced today.
DenNukem
+1  A: 

Hey, check out TTStyledLabel - it might do what you want.

DenNukem