自定义UISegmentedControl的颜色 [英] Customizing the colors of a UISegmentedControl
问题描述
有没有人知道如何根据UISegmentedControl自定义字符串的外观?我试图根据项目的选定状态设置单元格的背景颜色和文本颜色。
Does anybody know of a way to customize the appearance of the string based UISegmentedControl? I am trying to set the background color of the cell and the text color differently depending on the selected state of the item.
或者,你知道一种创建UIImages的方法,包括自定义字符串吗? (例如,创建具有白色背景的uiimage,覆盖文本,添加到分段控件)。
Alternatively, do you know of a way to create UIImages on the fly in which to include custom strings? (e.g. create uiimage with white background, overlay text, add to segmented control).
我知道你只能在分段控件中拥有字符串或图像......
I know that you can only have strings or images in the segmented control...
干杯!
推荐答案
UISegmentedControl有一个tintColor属性 - 这允许你改变控件的颜色,但不是一般的样式 (圆角,斜面形状):
UISegmentedControl has a tintColor property -- this allows you to change what color the control is, but not the general "style" (the rounded, beveled shape):
segmentedControl.tintColor = [UIColor blueColor];
至于动态创建UIImages,你可以创建一个CGContext,做你需要的任何绘图该上下文(包括字符串),然后从上下文的CGImage中获取UIImage:
As for creating UIImages on the fly, you can create a CGContext, do whatever drawing you need to in that context (including strings), and then get a UIImage out of the context's CGImage:
CGContextRef drawContext = CGBitmapContextCreate(<many parameters>);
//do drawing here
CGImageRef finalImage = CGBitmapContextCreateImage(drawContext);
UIImage *cellImage = [UIImage finalImage];
这篇关于自定义UISegmentedControl的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!