Skip to content

Commit 7ea7685

Browse files
committed
ADD: support for right-oriented segment images
1 parent 43e3e54 commit 7ea7685

File tree

4 files changed

+58
-17
lines changed

4 files changed

+58
-17
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ URBSegmentedControl
2626
Add the following to your `Podfile` and run `$ pod install`:
2727

2828
pod "URBSegmentedControl"
29-
29+
3030
If you don't have CocoaPods installed or integrated into your project, you can learn how to do via [CocoaPods](http://cocoapods.org/).
3131

3232
### Installing Manually
@@ -85,17 +85,18 @@ Your `URBSegmentedControl` can be customized using the following properties:
8585

8686
```objective-c
8787
// base styles
88-
@property (nonatomic, strong) UIColor *baseColor; // default [UIColor colorWithWhite:0.3 alpha:1.0]
89-
@property (nonatomic, strong) UIColor *strokeColor; // default [UIColor darkGrayColor]
90-
@property (nonatomic, assign) CGFloat strokeWidth; // default 2.0
91-
@property (nonatomic) CGFloat cornerRadius; // default 2.0
92-
@property (nonatomic, assign) UIEdgeInsets segmentEdgeInsets; // default UIEdgeInsetsMake(4.0, 4.0, 4.0, 4.0)
88+
@property (nonatomic, strong) UIColor *baseColor; // default [UIColor colorWithWhite:0.3 alpha:1.0]
89+
@property (nonatomic, strong) UIColor *strokeColor; // default [UIColor darkGrayColor]
90+
@property (nonatomic, assign) CGFloat strokeWidth; // default 2.0
91+
@property (nonatomic) CGFloat cornerRadius; // default 2.0
92+
@property (nonatomic, assign) UIEdgeInsets segmentEdgeInsets; // default UIEdgeInsetsMake(4.0, 4.0, 4.0, 4.0)
93+
@property (nonatomic, assign) URBSegmentImagePosition imagePosition; // default URBSegmentImagePositionLeft
9394

9495
// segment styles
95-
@property (nonatomic, strong) UIColor *segmentBackgroundColor; // default [UIColor redColor]
96-
@property (nonatomic, strong) UIColor *imageColor; // default [UIColor grayColor]
97-
@property (nonatomic, strong) UIColor *selectedImageColor; // default [UIColor whiteColor]
98-
@property (nonatomic, assign) BOOL showsGradient; // determines if the base and segment background should have a gradient applied, default YES
96+
@property (nonatomic, strong) UIColor *segmentBackgroundColor; // default [UIColor redColor]
97+
@property (nonatomic, strong) UIColor *imageColor; // default [UIColor grayColor]
98+
@property (nonatomic, strong) UIColor *selectedImageColor; // default [UIColor whiteColor]
99+
@property (nonatomic, assign) BOOL showsGradient; // determines if the base and segment background should have a gradient applied, default YES
99100
```
100101

101102
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`:
@@ -117,7 +118,7 @@ In most cases, the default insets applied to the content, title and image for ea
117118

118119
- Support setting images to use for background and segment states instead of drawing in CoreGraphics within the class
119120
- Support for UISegmentedControl's `momentary` mode
120-
- Better support for customization using UIAppearance
121+
~~- Better support for customization using UIAppearance~~
121122

122123
## Credits
123124

SampleProject/URBSegmentedControlDemo/AppDelegate.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
4646
//
4747
URBSegmentedControl *iconControl = [[URBSegmentedControl alloc] initWithItems:titles];
4848
iconControl.frame = CGRectMake(10.0, CGRectGetMaxY(control.frame) + 20.0, 300.0, 40.0);
49+
iconControl.imagePosition = URBSegmentImagePositionRight;
4950
[viewController.view addSubview:iconControl];
5051

5152
// set icons for each segment

URBSegmentedControl.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88

99
#import <UIKit/UIKit.h>
1010

11-
enum {
11+
typedef NS_ENUM(NSUInteger, URBSegmentedControlOrientation) {
1212
URBSegmentedControlOrientationHorizontal = 0,
1313
URBSegmentedControlOrientationVertical
1414
};
15-
typedef NSInteger URBSegmentedControlOrientation;
1615

17-
enum {
16+
typedef NS_ENUM(NSUInteger, URBSegmentViewLayout) {
1817
URBSegmentViewLayoutDefault = 0,
1918
URBSegmentViewLayoutVertical
2019
};
21-
typedef NSInteger URBSegmentViewLayout;
20+
21+
typedef NS_ENUM(NSUInteger, URBSegmentImagePosition) {
22+
URBSegmentImagePositionLeft = 0,
23+
URBSegmentImagePositionRight
24+
};
2225

2326
@interface URBSegmentedControl : UISegmentedControl <UIAppearance>
2427

@@ -34,6 +37,11 @@ typedef void (^URBSegmentedControlBlock)(NSInteger index, URBSegmentedControl *s
3437
*/
3538
@property (nonatomic) URBSegmentViewLayout segmentViewLayout;
3639

40+
/**
41+
* Position of the image when placed horizontally next to a segment label. Not used for controls containing only text or images.
42+
*/
43+
@property (nonatomic, assign) URBSegmentImagePosition imagePosition;
44+
3745
/**
3846
Block handle called when the selected segment has changed.
3947
*/
@@ -44,6 +52,8 @@ typedef void (^URBSegmentedControlBlock)(NSInteger index, URBSegmentedControl *s
4452
*/
4553
@property (nonatomic, strong) UIColor *baseColor;
4654

55+
@property (nonatomic, strong) UIColor *baseGradient;
56+
4757
/**
4858
Stroke color used around the base container view.
4959
*/
@@ -74,6 +84,7 @@ typedef void (^URBSegmentedControlBlock)(NSInteger index, URBSegmentedControl *s
7484
///----------------------------
7585

7686
@property (nonatomic, strong) UIColor *segmentBackgroundColor UI_APPEARANCE_SELECTOR;
87+
@property (nonatomic, strong) UIColor *segmentBackgroundGradient UI_APPEARANCE_SELECTOR;
7788
@property (nonatomic, strong) UIColor *imageColor UI_APPEARANCE_SELECTOR;
7889
@property (nonatomic, strong) UIColor *selectedImageColor UI_APPEARANCE_SELECTOR;
7990

URBSegmentedControl.m

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ - (UIColor *)adjustBrightness:(CGFloat)amount;
1515

1616
@interface URBSegmentView : UIButton
1717
@property (nonatomic, assign) URBSegmentViewLayout viewLayout;
18+
@property (nonatomic, assign) URBSegmentImagePosition imagePosition;
1819
@property (nonatomic, strong) UIColor *imageBackgroundColor;
1920
@property (nonatomic, strong) UIColor *selectedImageBackgroundColor;
2021
@property (nonatomic, assign) CGFloat cornerRadius;
@@ -65,6 +66,7 @@ - (void)initInternal{
6566
// layout
6667
self.layoutOrientation = URBSegmentedControlOrientationHorizontal;
6768
self.segmentViewLayout = URBSegmentViewLayoutDefault;
69+
self.imagePosition = URBSegmentImagePositionLeft;
6870

6971
// base image view
7072
_backgroundView = [[UIImageView alloc] init];
@@ -153,7 +155,9 @@ - (void)insertSegmentWithTitle:(NSString *)title image:(UIImage *)image atIndex:
153155

154156
// style the segment
155157
segmentView.viewLayout = self.segmentViewLayout;
158+
segmentView.imagePosition = self.imagePosition;
156159
segmentView.showsGradient = self.showsGradient;
160+
157161
if (self.segmentTextAttributes) {
158162
[segmentView setTextAttributes:self.segmentTextAttributes forState:UIControlStateNormal];
159163
}
@@ -237,6 +241,16 @@ - (void)setShowsGradient:(BOOL)showsGradient {
237241
}
238242
}
239243

244+
- (void)setImagePosition:(URBSegmentImagePosition)imagePosition {
245+
if (_imagePosition != imagePosition) {
246+
_imagePosition = imagePosition;
247+
248+
[self.items enumerateObjectsUsingBlock:^(URBSegmentView *segmentView, NSUInteger idx, BOOL *stop) {
249+
segmentView.imagePosition = imagePosition;
250+
}];
251+
}
252+
}
253+
240254
#pragma mark - UIKit API Overrides
241255

242256
- (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated {
@@ -674,8 +688,15 @@ - (void)layoutSubviews {
674688
}
675689
else {
676690
CGFloat titleX = CGRectGetWidth(frame) / 3.0;
677-
imageFrame = UIEdgeInsetsInsetRect(imageFrame, UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(frame) - titleX));
678-
titleFrame = UIEdgeInsetsInsetRect(titleFrame, UIEdgeInsetsMake(0, titleX + 2.0, 0, 0));
691+
692+
if (self.imagePosition == URBSegmentImagePositionRight) {
693+
imageFrame = UIEdgeInsetsInsetRect(imageFrame, UIEdgeInsetsMake(0, CGRectGetWidth(frame) - titleX, 0, 0));
694+
titleFrame = UIEdgeInsetsInsetRect(titleFrame, UIEdgeInsetsMake(0, 0, 0, titleX + 2.0));
695+
}
696+
else {
697+
imageFrame = UIEdgeInsetsInsetRect(imageFrame, UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(frame) - titleX));
698+
titleFrame = UIEdgeInsetsInsetRect(titleFrame, UIEdgeInsetsMake(0, titleX + 2.0, 0, 0));
699+
}
679700
}
680701
}
681702
self.imageView.frame = imageFrame;
@@ -732,6 +753,13 @@ - (void)setShowsGradient:(BOOL)showsGradient {
732753
}
733754
}
734755

756+
- (void)setImagePosition:(URBSegmentImagePosition)imagePosition {
757+
if (_imagePosition != imagePosition) {
758+
_imagePosition = imagePosition;
759+
[self setNeedsLayout];
760+
}
761+
}
762+
735763
#pragma mark - Background Images
736764

737765
- (void)updateBackgrounds {

0 commit comments

Comments
 (0)