Skip to content

Commit e489fd0

Browse files
committed
integrated bug fixes/additions from @nowsprinting, fixed bugs with showGradient
1 parent e43bad5 commit e489fd0

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,17 @@ Your `URBSegmentedControl` can be customized using the following properties:
7474

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

8283
// segment styles
8384
@property (nonatomic, strong) UIColor *segmentBackgroundColor; // default [UIColor redColor]
8485
@property (nonatomic, strong) UIColor *imageColor; // default [UIColor grayColor]
8586
@property (nonatomic, strong) UIColor *selectedImageColor; // default [UIColor whiteColor]
86-
@property (nonatomic, assign) BOOL showsGradient; // determines if the segment background should have a gradient applied, default YES
87+
@property (nonatomic, assign) BOOL showsGradient; // determines if the base and segment background should have a gradient applied, default YES
8788
```
8889

8990
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`:

URBSegmentedControl.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,51 @@ typedef NSInteger URBSegmentViewLayout;
2424

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

27+
/**
28+
Layout behavior for the segments (row or columns).
29+
*/
2730
@property (nonatomic) URBSegmentedControlOrientation layoutOrientation;
31+
32+
/**
33+
Layout behavior of the segment contents.
34+
*/
2835
@property (nonatomic) URBSegmentViewLayout segmentViewLayout;
2936

37+
/**
38+
Block handle called when the selected segment has changed.
39+
*/
3040
@property (nonatomic, copy) URBSegmentedControlBlock controlEventBlock;
3141

42+
/**
43+
Background color for the base container view.
44+
*/
3245
@property (nonatomic, strong) UIColor *baseColor;
46+
47+
/**
48+
Stroke color used around the base container view.
49+
*/
3350
@property (nonatomic, strong) UIColor *strokeColor;
51+
52+
/**
53+
Stroke width for the base container view.
54+
*/
3455
@property (nonatomic, assign) CGFloat strokeWidth;
56+
57+
/**
58+
Corner radius for the base container view.
59+
*/
3560
@property (nonatomic) CGFloat cornerRadius;
3661

62+
/**
63+
Whether or not a gradient should be automatically applied to the base and segment backgrounds based on the defined base colors.
64+
*/
65+
@property (nonatomic, assign) BOOL showsGradient;
66+
67+
/**
68+
Padding between the segments and the base container view.
69+
*/
70+
@property (nonatomic, assign) UIEdgeInsets segmentEdgeInsets;
71+
3772
///----------------------------
3873
/// @name Segment Customization
3974
///----------------------------
@@ -45,7 +80,6 @@ typedef void (^URBSegmentedControlBlock)(NSInteger index, URBSegmentedControl *s
4580
@property (nonatomic, assign) UIEdgeInsets contentEdgeInsets;
4681
@property (nonatomic, assign) UIEdgeInsets titleEdgeInsets;
4782
@property (nonatomic, assign) UIEdgeInsets imageEdgeInsets;
48-
@property (nonatomic, assign) BOOL showsGradient;
4983

5084
- (id)initWithTitles:(NSArray *)titles;
5185
- (id)initWithIcons:(NSArray *)icons;

URBSegmentedControl.m

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ - (void)updateBackgrounds;
2626
@interface URBSegmentedControl ()
2727
@property (nonatomic, strong) NSMutableArray *items;
2828
@property (nonatomic, strong) UIImageView *backgroundView;
29-
@property (nonatomic) UIEdgeInsets segmentEdgeInsets;
3029
@property (nonatomic, strong) NSDictionary *segmentTextAttributes;
30+
@property (nonatomic, strong) NSDictionary *segmentDisabledTextAttributes;
3131
- (void)layoutSegments;
3232
- (void)handleSelect:(URBSegmentView *)segmentView;
3333
- (NSInteger)firstSegmentIndexNearIndex:(NSUInteger)index enabled:(BOOL)enabled;
@@ -55,11 +55,12 @@ - (void)initInternal{
5555
self.segmentEdgeInsets = UIEdgeInsetsMake(4.0f, 4.0f, 4.0f, 4.0f);
5656

5757
// base styles
58-
self.baseColor = [UIColor colorWithRed:0.35 green:0.35 blue:0.35 alpha:1.0];
58+
self.baseColor = [UIColor colorWithWhite:0.3 alpha:1.0];
5959
self.strokeColor = [UIColor darkGrayColor];
6060
self.segmentBackgroundColor = nil;
6161
self.strokeWidth = 2.0f;
6262
self.cornerRadius = 4.0f;
63+
self.showsGradient = YES;
6364

6465
// layout
6566
self.layoutOrientation = URBSegmentedControlOrientationHorizontal;
@@ -116,16 +117,15 @@ - (id)initWithTitles:(NSArray *)titles icons:(NSArray *)icons {
116117
return self;
117118
}
118119

119-
/** Initialize from nib file */
120120
- (id)initWithCoder:(NSCoder *)aDecoder {
121121
self = [super initWithCoder:aDecoder];
122-
if (self){
122+
if (self) {
123123
CGRect nibFrame = self.frame;
124124
[self initInternal];
125125

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

154154
// style the segment
155155
segmentView.viewLayout = self.segmentViewLayout;
156+
segmentView.showsGradient = self.showsGradient;
156157
if (self.segmentTextAttributes) {
157158
[segmentView setTextAttributes:self.segmentTextAttributes forState:UIControlStateNormal];
158159
}
160+
if (self.segmentDisabledTextAttributes) {
161+
[segmentView setTextAttributes:self.segmentDisabledTextAttributes forState:UIControlStateDisabled];
162+
}
159163

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

323327
- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex {
324328
if (_selectedSegmentIndex != selectedSegmentIndex) {
325-
NSParameterAssert(selectedSegmentIndex < (NSInteger)self.items.count);
329+
NSParameterAssert(selectedSegmentIndex < (NSInteger)self.items.count && selectedSegmentIndex >= 0);
326330

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

367371
- (void)setTextAttributes:(NSDictionary *)textAttributes forState:(UIControlState)state {
368-
self.segmentTextAttributes = textAttributes;
372+
if (state == UIControlStateDisabled)
373+
self.segmentDisabledTextAttributes = textAttributes;
374+
else
375+
self.segmentTextAttributes = textAttributes;
376+
377+
[self.items enumerateObjectsUsingBlock:^(URBSegmentView *segmentView, NSUInteger idx, BOOL *stop) {
378+
[segmentView setTextAttributes:textAttributes forState:state];
379+
}];
369380
}
370381

371382
- (void)setSegmentBackgroundColor:(UIColor *)segmentBackgroundColor {
@@ -401,10 +412,10 @@ - (UIImage *)defaultBackgroundImage {
401412

402413
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
403414
CGContextRef context = UIGraphicsGetCurrentContext();
404-
415+
405416
// colors
406-
UIColor* baseGradientTopColor = [self.baseColor adjustBrightness:1.1];
407-
UIColor* baseGradientBottomColor = [self.baseColor adjustBrightness:0.9];
417+
UIColor* baseGradientTopColor = (self.showsGradient) ? [self.baseColor adjustBrightness:1.1] : self.baseColor;
418+
UIColor* baseGradientBottomColor = (self.showsGradient) ? [self.baseColor adjustBrightness:0.9] : self.baseColor;
408419
UIColor* baseStrokeColor = self.strokeColor;
409420

410421
// gradients

0 commit comments

Comments
 (0)