如何更改 UIPickerView 行中单个字符的字体颜色?
我有一个应用程序,它根据搜索在 UIPickerView 中显示文本。我想突出显示字符串中因通配符而出现的特定字母。
例如,如果我搜索“CA?”,其中一行将显示“CAT”,而我只希望字母“T”为蓝色。
有什么想法吗?用户在打字时会立即得到反馈,因此性能很重要。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要创建一个针对不同字符具有不同字体属性的字符串,通常会使用
NSAttributedString
。但是,UIPickerView
似乎不直接支持使用NSAttributedString
作为选择器组件的标签,UILabel
似乎也不支持它们。您可能必须创建一个自定义UIView
子类,并从UIPickerViewDelegate
中的pickerView:viewForRow:forComponent:reusingView:
返回它。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 usingNSAttributedString
s as the labels for your picker components, nor doesUILabel
seem to support them. You might have to create a customUIView
subclass and return it frompickerView:viewForRow:forComponent:reusingView:
in yourUIPickerViewDelegate
.感谢大卫给我的建议让我开始。
我最终使用 Three20 库并从 pickerView:viewForRow:forComponent:reusingView: 返回 TTStyledTextLabel,其中文本属性设置为 [TTStyledText textFromXHTML:myXHTML] 以及 TTDefaultStyleSheet 来定义彩色跨度。在 UIPickerView 组件中工作得很好并且似乎非常快。
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.