How to change font color of individual characters in a UIPickerView row?
I have an app which shows text in a UIPickerView based on a search. I want to highlight specific letters in the string that are there as a result of a wildcard character.
开发者_JS百科For example, if I searched for "CA?", one of the rows will show "CAT" and I want only the letter "T" to be in the color blue.
Any ideas? The user gets immediate feedback as he types so performance is important.
To create a string that has different font properties for different characters, you would generally use NSAttributedString
. However, UIPickerView
doesn't seem to directly support using NSAttributedString
s as the labels for your picker components, nor does UILabel
seem to support them. You might have to create a custom UIView
subclass and return it from pickerView:viewForRow:forComponent:reusingView:
in your UIPickerViewDelegate
.
Thanks to David for the tip to get me started.
I ended up using the Three20 library and returning a TTStyledTextLabel from pickerView:viewForRow:forComponent:reusingView: with the text property set to [TTStyledText textFromXHTML:myXHTML] along with a TTDefaultStyleSheet to define the colored spans. Works great and seems to be very fast in the UIPickerView component.
精彩评论