Skip to content

Instantly share code, notes, and snippets.

@boucher
Created November 4, 2008 20:41
Show Gist options
  • Select an option

  • Save boucher/22208 to your computer and use it in GitHub Desktop.

Select an option

Save boucher/22208 to your computer and use it in GitHub Desktop.
_styleControl = [[FormatSegmentedControl alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 18.0)];
[_styleControl setTrackingMode:CPSegmentSwitchTrackingSelectAny];
[_styleControl setSegmentCount:3];
[_styleControl setAction:@selector(changeTextStyle:)];
[_styleControl setWidth:22.0 forSegment:0];
[_styleControl setImage:FormattingBoldImage forSegment:0];
[_styleControl setTag:0 forSegment:0];
[_styleControl setWidth:22.0 forSegment:1];
[_styleControl setImage:FormattingItalicImage forSegment:1];
[_styleControl setTag:1 forSegment:1];
[_styleControl setWidth:22.0 forSegment:2];
[_styleControl setImage:FormattingUnderlineImage forSegment:2];
[_styleControl setTag:2 forSegment:2];
[_styleControl setFrameOrigin:CGPointMake(CONTROL_MARGIN + CGRectGetMaxX([borderView frame]), (height - CGRectGetHeight([_styleControl frame])) / 2.0)];
[self addSubview:_styleControl];
var FormatSegmentedControlLeftCapColors = nil,
FormatSegmentedControlCenterColors = nil,
FormatSegmentedControlRightCapColors = nil;
@implementation FormatSegmentedControl : CPSegmentedControl
{
CPArray _backgroundViews;
}
+ (void)initialize
{
if (self != [FormatSegmentedControl class])
return;
// Make sure FormatBar is initialized.
[FormatBar class];
FormatSegmentedControlLeftCapColors =
[
[CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[FormatBarButtonLeftImage, FormatBarButtonCenterImage, nil] isVertical:NO]],
[CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[FormatBarButtonHighlightedLeftImage, FormatBarButtonHighlightedCenterImage, nil] isVertical:NO]],
[CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[FormatBarButtonSelectedLeftImage, FormatBarButtonSelectedCenterImage, nil] isVertical:NO]]
];
FormatSegmentedControlCenterColors =
[
[CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[FormatBarButtonDividerImage, FormatBarButtonCenterImage, nil] isVertical:NO]],
[CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[FormatBarButtonDividerImage, FormatBarButtonHighlightedCenterImage, nil] isVertical:NO]],
[CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[FormatBarButtonDividerImage, FormatBarButtonSelectedCenterImage, nil] isVertical:NO]]
];
FormatSegmentedControlRightCapColors =
[
[CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[FormatBarButtonDividerImage, FormatBarButtonCenterImage, FormatBarButtonRightImage] isVertical:NO]],
[CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[FormatBarButtonDividerImage, FormatBarButtonHighlightedCenterImage, FormatBarButtonHighlightedRightImage] isVertical:NO]],
[CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[FormatBarButtonDividerImage, FormatBarButtonSelectedCenterImage, FormatBarButtonSelectedRightImage] isVertical:NO]]
];
}
- (void)drawSegmentBezel:(unsigned)aSegment highlight:(BOOL)shouldHighlight
{
if (!_backgroundViews)
_backgroundViews = [];
if (!_backgroundViews[aSegment])
{
_backgroundViews[aSegment] = [[CPView alloc] initWithFrame:[self frameForSegment:aSegment]];
[self addSubview:_backgroundViews[aSegment] positioned:CPWindowBelow relativeTo:nil];
}
else
[_backgroundViews[aSegment] setFrame:[self frameForSegment:aSegment]];
var index = shouldHighlight ? 1 : [self isSelectedForSegment:aSegment] ? 2 : 0;
if (aSegment == 0)
[_backgroundViews[aSegment] setBackgroundColor:FormatSegmentedControlLeftCapColors[index]];
else if (aSegment == [self segmentCount] - 1)
[_backgroundViews[aSegment] setBackgroundColor:FormatSegmentedControlRightCapColors[index]];
else
[_backgroundViews[aSegment] setBackgroundColor:FormatSegmentedControlCenterColors[index]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment