Skip to content

Instantly share code, notes, and snippets.

@cappuccino
Created March 18, 2009 19:26
Show Gist options
  • Select an option

  • Save cappuccino/81333 to your computer and use it in GitHub Desktop.

Select an option

Save cappuccino/81333 to your computer and use it in GitHub Desktop.
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
_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:[[CPImage alloc] initWithContentsOfFile:@"http://280slides.com/Editor/Current/Resources/FormatBar/FormattingBold.png" size:CGSizeMake(6, 7)] forSegment:0];
[_styleControl setTag:0 forSegment:0];
[_styleControl setWidth:22.0 forSegment:1];
[_styleControl setImage:[[CPImage alloc] initWithContentsOfFile:@"http://280slides.com/Editor/Current/Resources/FormatBar/FormattingItalic.png" size:CGSizeMake(6, 7)] forSegment:1];
[_styleControl setTag:1 forSegment:1];
[_styleControl setWidth:22.0 forSegment:2];
[_styleControl setImage:[[CPImage alloc] initWithContentsOfFile:@"http://280slides.com/Editor/Current/Resources/FormatBar/FormattingUnderline.png" size:CGSizeMake(6, 7)] forSegment:2];
[_styleControl setTag:2 forSegment:2];
[_styleControl setFrameOrigin:CGPointMake(5 , 5)];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment