Skip to content

Commit

Permalink
Merge pull request Grouper#179 from linzhongsc/master
Browse files Browse the repository at this point in the history
Configurable highlighted background and add touch events
  • Loading branch information
jamieomatthews committed Apr 6, 2015
2 parents 9f75d96 + d340f7b commit f2f4b6c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Classes/ios/FUISegmentedControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
@property(nonatomic, strong, readwrite) UIColor *selectedColor UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIColor *disabledColor UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIColor *deselectedColor UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIColor *highlightedColor UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIColor *dividerColor UI_APPEARANCE_SELECTOR;
@property(nonatomic, readwrite) CGFloat cornerRadius UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIFont *selectedFont UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIFont *disabledFont UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIFont *deselectedFont UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIFont *highlightedFont UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIColor *selectedFontColor UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIColor *deselectedFontColor UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIColor *disabledFontColor UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIColor *highlightedFontColor UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong, readwrite) UIColor *borderColor UI_APPEARANCE_SELECTOR;
@property(nonatomic, readwrite) CGFloat borderWidth UI_APPEARANCE_SELECTOR;

Expand Down
81 changes: 80 additions & 1 deletion Classes/ios/FUISegmentedControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ + (void)initialize {
[appearance setDeselectedColor:[UIColor darkGrayColor]];
[appearance setSelectedFontColor:[UIColor whiteColor]];
[appearance setDeselectedFontColor:[UIColor whiteColor]];
[appearance setHighlightedColor:[UIColor grayColor]];
[appearance setHighlightedFontColor:[UIColor whiteColor]];
}
}

Expand All @@ -34,6 +36,11 @@ - (void)setSelectedColor:(UIColor *)selectedColor {
[self configureFlatSegmentedControl];
}

- (void)setHighlightedColor:(UIColor *)highlightedColor {
_highlightedColor = highlightedColor;
[self configureFlatSegmentedControl];
}

- (void)setDisabledColor:(UIColor *)disabledColor {
_disabledColor = disabledColor;
[self configureFlatSegmentedControl];
Expand Down Expand Up @@ -79,6 +86,16 @@ - (void)setDisabledFontColor:(UIColor *)disabledFontColor {
[self setupFonts];
}

- (void)setHighlightedFont:(UIFont *)highlightedFont {
_highlightedFont = highlightedFont;
[self setupFonts];
}

- (void)setHighlightedFontColor:(UIColor *)highlightedFontColor {
_highlightedFontColor = highlightedFontColor;
[self setupFonts];
}

- (void)setBorderColor:(UIColor *)borderColor {
_borderColor = borderColor;
[self configureFlatSegmentedControl];
Expand Down Expand Up @@ -183,6 +200,37 @@ - (void)setupFonts {
}

[self setTitleTextAttributes:disabledAttributesDictionary forState:UIControlStateDisabled];


NSDictionary *highlightedAttributesDictionary;
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
// iOS7+ methods
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:CGSizeZero];
[shadow setShadowColor:[UIColor clearColor]];
highlightedAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
self.highlightedFontColor,
NSForegroundColorAttributeName,
shadow,
NSShadowAttributeName,
self.highlightedFont,
NSFontAttributeName,
nil];
} else {
// iOS6- methods
highlightedAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
self.highlightedFontColor,
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
UITextAttributeTextShadowOffset,
self.highlightedFont,
UITextAttributeFont,
nil];
}

[self setTitleTextAttributes:highlightedAttributesDictionary forState:UIControlStateHighlighted];
}

- (void)configureFlatSegmentedControl {
Expand All @@ -198,7 +246,10 @@ - (void)configureFlatSegmentedControl {
cornerRadius:self.cornerRadius
shadowColor:[UIColor clearColor]
shadowInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

UIImage *highlightedBackgroundImage = [UIImage buttonImageWithColor:self.highlightedColor
cornerRadius:self.cornerRadius
shadowColor:[UIColor clearColor]
shadowInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

UIColor *selectedColor = (self.dividerColor) ? self.dividerColor : self.selectedColor;
UIColor *deselectedColor = (self.dividerColor) ? self.dividerColor : self.deselectedColor;
Expand All @@ -209,6 +260,7 @@ - (void)configureFlatSegmentedControl {
[self setBackgroundImage:selectedBackgroundImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:deselectedBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:disabledBackgroundImage forState:UIControlStateDisabled barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:highlightedBackgroundImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

[self setDividerImage:deselectedDividerImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setDividerImage:selectedDividerImage forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Expand All @@ -219,4 +271,31 @@ - (void)configureFlatSegmentedControl {
self.layer.cornerRadius = self.cornerRadius;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSInteger previousSelectedSegmentIndex = self.selectedSegmentIndex;
[super touchesBegan:touches withEvent:event];
[self sendActionsForControlEvents:UIControlEventTouchDown];
if (!(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)) {
// before iOS7 the segment is selected in touchesBegan
if (previousSelectedSegmentIndex == self.selectedSegmentIndex) {
// if the selectedSegmentIndex before the selection process is equal to the selectedSegmentIndex
// after the selection process the superclass won't send a UIControlEventValueChanged event.
// So we have to do this ourselves.
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSInteger previousSelectedSegmentIndex = self.selectedSegmentIndex;
[super touchesEnded:touches withEvent:event];
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
// on iOS7 the segment is selected in touchesEnded
if (previousSelectedSegmentIndex == self.selectedSegmentIndex) {
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
}
}

@end

0 comments on commit f2f4b6c

Please sign in to comment.