简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM