开发者

How to change font color of individual characters in a UIPickerView row?

开发者 https://www.devze.com 2023-01-10 09:53 出处:网络
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.

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 NSAttributedStrings 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号