Skip to content

Commit

Permalink
integrated bug fixes/additions from @nowsprinting, fixed bugs with sh…
Browse files Browse the repository at this point in the history
…owGradient
  • Loading branch information
u10int committed Mar 4, 2013
1 parent e43bad5 commit e489fd0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 17 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,17 @@ Your `URBSegmentedControl` can be customized using the following properties:

```objective-c
// base styles
@property (nonatomic, strong) UIColor *baseColor; // default [UIColor colorWithRed:0.35 green:0.35 blue:0.35 alpha:1.0];
@property (nonatomic, strong) UIColor *strokeColor; // default [UIColor darkGrayColor]
@property (nonatomic, assign) CGFloat strokeWidth; // default 2.0
@property (nonatomic) CGFloat cornerRadius; // default 2.0
@property (nonatomic, strong) UIColor *baseColor; // default [UIColor colorWithWhite:0.3 alpha:1.0]
@property (nonatomic, strong) UIColor *strokeColor; // default [UIColor darkGrayColor]
@property (nonatomic, assign) CGFloat strokeWidth; // default 2.0
@property (nonatomic) CGFloat cornerRadius; // default 2.0
@property (nonatomic, assign) UIEdgeInsets segmentEdgeInsets; // default UIEdgeInsetsMake(4.0, 4.0, 4.0, 4.0)

// segment styles
@property (nonatomic, strong) UIColor *segmentBackgroundColor; // default [UIColor redColor]
@property (nonatomic, strong) UIColor *imageColor; // default [UIColor grayColor]
@property (nonatomic, strong) UIColor *selectedImageColor; // default [UIColor whiteColor]
@property (nonatomic, assign) BOOL showsGradient; // determines if the segment background should have a gradient applied, default YES
@property (nonatomic, assign) BOOL showsGradient; // determines if the base and segment background should have a gradient applied, default YES
```

By default, your images will be tinted with the colors you define using the `imageColor` and `selectedImageColor` properties. If you would rather keep your images in their original format, just set these color properties to `nil`:
Expand Down
36 changes: 35 additions & 1 deletion URBSegmentedControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,51 @@ typedef NSInteger URBSegmentViewLayout;

typedef void (^URBSegmentedControlBlock)(NSInteger index, URBSegmentedControl *segmentedControl);

/**
Layout behavior for the segments (row or columns).
*/
@property (nonatomic) URBSegmentedControlOrientation layoutOrientation;

/**
Layout behavior of the segment contents.
*/
@property (nonatomic) URBSegmentViewLayout segmentViewLayout;

/**
Block handle called when the selected segment has changed.
*/
@property (nonatomic, copy) URBSegmentedControlBlock controlEventBlock;

/**
Background color for the base container view.
*/
@property (nonatomic, strong) UIColor *baseColor;

/**
Stroke color used around the base container view.
*/
@property (nonatomic, strong) UIColor *strokeColor;

/**
Stroke width for the base container view.
*/
@property (nonatomic, assign) CGFloat strokeWidth;

/**
Corner radius for the base container view.
*/
@property (nonatomic) CGFloat cornerRadius;

/**
Whether or not a gradient should be automatically applied to the base and segment backgrounds based on the defined base colors.
*/
@property (nonatomic, assign) BOOL showsGradient;

/**
Padding between the segments and the base container view.
*/
@property (nonatomic, assign) UIEdgeInsets segmentEdgeInsets;

///----------------------------
/// @name Segment Customization
///----------------------------
Expand All @@ -45,7 +80,6 @@ typedef void (^URBSegmentedControlBlock)(NSInteger index, URBSegmentedControl *s
@property (nonatomic, assign) UIEdgeInsets contentEdgeInsets;
@property (nonatomic, assign) UIEdgeInsets titleEdgeInsets;
@property (nonatomic, assign) UIEdgeInsets imageEdgeInsets;
@property (nonatomic, assign) BOOL showsGradient;

- (id)initWithTitles:(NSArray *)titles;
- (id)initWithIcons:(NSArray *)icons;
Expand Down
33 changes: 22 additions & 11 deletions URBSegmentedControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ - (void)updateBackgrounds;
@interface URBSegmentedControl ()
@property (nonatomic, strong) NSMutableArray *items;
@property (nonatomic, strong) UIImageView *backgroundView;
@property (nonatomic) UIEdgeInsets segmentEdgeInsets;
@property (nonatomic, strong) NSDictionary *segmentTextAttributes;
@property (nonatomic, strong) NSDictionary *segmentDisabledTextAttributes;
- (void)layoutSegments;
- (void)handleSelect:(URBSegmentView *)segmentView;
- (NSInteger)firstSegmentIndexNearIndex:(NSUInteger)index enabled:(BOOL)enabled;
Expand Down Expand Up @@ -55,11 +55,12 @@ - (void)initInternal{
self.segmentEdgeInsets = UIEdgeInsetsMake(4.0f, 4.0f, 4.0f, 4.0f);

// base styles
self.baseColor = [UIColor colorWithRed:0.35 green:0.35 blue:0.35 alpha:1.0];
self.baseColor = [UIColor colorWithWhite:0.3 alpha:1.0];
self.strokeColor = [UIColor darkGrayColor];
self.segmentBackgroundColor = nil;
self.strokeWidth = 2.0f;
self.cornerRadius = 4.0f;
self.showsGradient = YES;

// layout
self.layoutOrientation = URBSegmentedControlOrientationHorizontal;
Expand Down Expand Up @@ -116,16 +117,15 @@ - (id)initWithTitles:(NSArray *)titles icons:(NSArray *)icons {
return self;
}

/** Initialize from nib file */
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self){
if (self) {
CGRect nibFrame = self.frame;
[self initInternal];

//Restore nib settings
// restore nib settings
self.frame = nibFrame;
for(NSInteger i=0; i<super.numberOfSegments; i++){
for (NSInteger i = 0; i < super.numberOfSegments; i++){
[self insertSegmentWithTitle:[super titleForSegmentAtIndex:i] atIndex:i animated:NO];
}
}
Expand Down Expand Up @@ -153,9 +153,13 @@ - (void)insertSegmentWithTitle:(NSString *)title image:(UIImage *)image atIndex:

// style the segment
segmentView.viewLayout = self.segmentViewLayout;
segmentView.showsGradient = self.showsGradient;
if (self.segmentTextAttributes) {
[segmentView setTextAttributes:self.segmentTextAttributes forState:UIControlStateNormal];
}
if (self.segmentDisabledTextAttributes) {
[segmentView setTextAttributes:self.segmentDisabledTextAttributes forState:UIControlStateDisabled];
}

// set custom styles if defined
segmentView.cornerRadius = self.cornerRadius;
Expand Down Expand Up @@ -322,7 +326,7 @@ - (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment {

- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex {
if (_selectedSegmentIndex != selectedSegmentIndex) {
NSParameterAssert(selectedSegmentIndex < (NSInteger)self.items.count);
NSParameterAssert(selectedSegmentIndex < (NSInteger)self.items.count && selectedSegmentIndex >= 0);

// deselect current segment if selected
if (_selectedSegmentIndex >= 0)
Expand Down Expand Up @@ -365,7 +369,14 @@ - (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment {
#pragma mark - Customization

- (void)setTextAttributes:(NSDictionary *)textAttributes forState:(UIControlState)state {
self.segmentTextAttributes = textAttributes;
if (state == UIControlStateDisabled)
self.segmentDisabledTextAttributes = textAttributes;
else
self.segmentTextAttributes = textAttributes;

[self.items enumerateObjectsUsingBlock:^(URBSegmentView *segmentView, NSUInteger idx, BOOL *stop) {
[segmentView setTextAttributes:textAttributes forState:state];
}];
}

- (void)setSegmentBackgroundColor:(UIColor *)segmentBackgroundColor {
Expand Down Expand Up @@ -401,10 +412,10 @@ - (UIImage *)defaultBackgroundImage {

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();

// colors
UIColor* baseGradientTopColor = [self.baseColor adjustBrightness:1.1];
UIColor* baseGradientBottomColor = [self.baseColor adjustBrightness:0.9];
UIColor* baseGradientTopColor = (self.showsGradient) ? [self.baseColor adjustBrightness:1.1] : self.baseColor;
UIColor* baseGradientBottomColor = (self.showsGradient) ? [self.baseColor adjustBrightness:0.9] : self.baseColor;
UIColor* baseStrokeColor = self.strokeColor;

// gradients
Expand Down

0 comments on commit e489fd0

Please sign in to comment.