nic*_*mro 24 objective-c uisegmentedcontrol ios
有没有人知道如何自定义基于UISegmentedControl的字符串的外观?我试图根据项目的选定状态设置单元格的背景颜色和文本颜色.
或者,你知道一种创建UIImages的方法,包括自定义字符串吗?(例如,创建具有白色背景的uiimage,覆盖文本,添加到分段控件).
我知道你只能在分段控件中使用字符串或图像...
干杯!
Arc*_*ite 21
UISegmentedControl具有tintColor属性 - 这允许您更改控件的颜色,但不能更改一般的"样式"(圆角,斜角形状):
segmentedControl.tintColor = [UIColor blueColor];
Run Code Online (Sandbox Code Playgroud)
至于在飞行创建UIImages,你可以创建一个CGContext上,做任何绘图时,需要在这方面(包括字符串),然后得到一个UIImage了上下文的CGImage的:
CGContextRef drawContext = CGBitmapContextCreate(<many parameters>);
//do drawing here
CGImageRef finalImage = CGBitmapContextCreateImage(drawContext);
UIImage *cellImage = [UIImage finalImage];
Run Code Online (Sandbox Code Playgroud)
jxd*_*ter 12
segmentedControl.tintColor = [UIColor colorWithRed:0.61176f green:0.61176f blue:0.61176f alpha:1.0f];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
Run Code Online (Sandbox Code Playgroud)
Por*_*ner 10
这里的大多数答案都没有回答如何基于所选状态设置按钮颜色的具体问题,这意味着对于未选择状态需要另一种颜色.我在这方面挣扎了很长时间,并希望与其他人分享我的解决方案.
我的例子使用了UISegmentedControl
三个段.所有三种颜色的未选择颜色应该相同,以使其具有统一的外观.第一个和最后一个段的选定状态具有唯一的颜色.
问题是分段控件不能保证在同一个顺序中,所以当你来回选择时颜色会混淆.Dan发布了一个使用标签的解决方案,但不幸的是,它不再保证适用于iOS 6及更高版本.
大部分代码都来自这篇文章.我略微改变它以获得独特的选择颜色.
它的工作原理是排序,但要注意这两个重要的行来设置所选的颜色:
NSInteger selectedIdx = betterSegmentedControl.selectedSegmentIndex;
[[sortedViews objectAtIndex:selectedIdx] setTintColor:[self.segmentColors objectAtIndex:selectedIdx]];
Run Code Online (Sandbox Code Playgroud)
- (void) updateSegmentColors
{
UIColor *checkColor = [UIColor colorWithRed: 29/255.0 green:166/255.0 blue:47/255.0 alpha:1.0];
NSArray *segmentColors = [[NSArray alloc] initWithObjects:checkColor, [UIColor blueColor], [UIColor redColor], nil];
UISegmentedControl *betterSegmentedControl = self.StatusControl;
// Get number of segments
NSUInteger numSegments = [betterSegmentedControl.subviews count];
// Reset segment's color (non selected color)
for( int i = 0; i < numSegments; i++ ) {
// reset color
[[betterSegmentedControl.subviews objectAtIndex:i] setTintColor:nil];
[[betterSegmentedControl.subviews objectAtIndex:i] setTintColor:[UIColor blueColor]];
}
// Sort segments from left to right
NSArray *sortedViews = [betterSegmentedControl.subviews sortedArrayUsingFunction:compareViewsByOrigin context:NULL];
// Change color of selected segment
NSInteger selectedIdx = betterSegmentedControl.selectedSegmentIndex;
[[sortedViews objectAtIndex:selectedIdx] setTintColor:[self.segmentColors objectAtIndex:selectedIdx]];
// Remove all original segments from the control
for (id view in betterSegmentedControl.subviews) {
[view removeFromSuperview];
}
// Append sorted and colored segments to the control
for (id view in sortedViews) {
[betterSegmentedControl addSubview:view];
}
}
NSInteger static compareViewsByOrigin(id sp1, id sp2, void *context)
{
// UISegmentedControl segments use UISegment objects (private API). But we can safely cast them to UIView objects.
float v1 = ((UIView *)sp1).frame.origin.x;
float v2 = ((UIView *)sp2).frame.origin.x;
if (v1 < v2)
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}
Run Code Online (Sandbox Code Playgroud)
我将代码放在它自己的方法中,因为我在表视图中加载这些分段控件,需要在加载(存储中的现有状态)和用户更改选择时运行它.现在我只需要[Self updateSegmentColors];
在事情发生变化时打电话.
小智 9
你所要做的就是:
// Get an array of the subviews of a UISegmentedControl, for example myUISegmentedControl:
NSArray *arri = [myUISegmentedControl subviews];
// Change the tintColor of each subview within the array:
[[arri objectAtIndex:0] setTintColor:[UIColor redColor]];
[[arri objectAtIndex:1] setTintColor:[UIColor greenColor]];
Run Code Online (Sandbox Code Playgroud)
我发现做这样的事情的最好方法是在分段控件上为不同的UIControlStates设置不同的属性.
self.segmentedControl.tintColor = [UIColor cb_Grey1Color];
self.segmentedControl.backgroundColor = [UIColor cb_Grey3Color];
NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont cbGothamBookFontWithSize:13.0], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIColor cb_Grey1Color], NSBackgroundColorAttributeName, nil];
[self.segmentedControl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
NSDictionary *unselectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont cbGothamBookFontWithSize:13.0], NSFontAttributeName,
[UIColor cb_Grey2Color], NSForegroundColorAttributeName,
[UIColor cb_Grey3Color], NSBackgroundColorAttributeName, nil];
[self.segmentedControl setTitleTextAttributes:unselectedAttributes forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
48510 次 |
最近记录: |