Skip to content

Instantly share code, notes, and snippets.

@happyrobots
Created January 2, 2014 11:16
Show Gist options
  • Save happyrobots/8217793 to your computer and use it in GitHub Desktop.
Save happyrobots/8217793 to your computer and use it in GitHub Desktop.
@interface UISegmentedControl (iOS7Flat)
- (void)flattenUIWithFont:(UIFont *)font tintColor:(UIColor *)tintColor highlightedTextColor:(UIColor *)highlightedTextColor;
@end
#import "UISegmentedControl+iOS7Flat.h"
@interface UIImage (Additions)
+ (UIImage *)imageWithColor:(UIColor *)color;
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size andRoundSize:(CGFloat)roundSize;
@end
@implementation UIImage (Additions)
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size andRoundSize:(CGFloat)roundSize {
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
if (roundSize > 0) {
UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius: roundSize];
[color setFill];
[roundedRectanglePath fill];
} else {
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
@implementation UISegmentedControl (iOS7Flat)
- (void)flattenUIWithFont:(UIFont *)font tintColor:(UIColor *)tintColor highlightedTextColor:(UIColor *)highlightedTextColor
{
CGRect f = self.frame;
f.size.height -= font.pointSize; // height minus font size, to prevent big segmented control on iOS 6
self.frame = f;
self.layer.cornerRadius = 5.0f;
self.layer.borderWidth = 1.0f;
self.layer.borderColor = tintColor.CGColor;
UIImage *normalBackgroundImage = [UIImage imageWithColor:tintColor size:CGSizeMake(10.0f, 40.0f) andRoundSize:5.0f];
UIImage *selectedBackgroundImage = [UIImage imageWithColor:[UIColor clearColor] size:CGSizeMake(10.0f, 40.0f) andRoundSize:5.0f];
NSDictionary *attributes = @{UITextAttributeFont: font,
UITextAttributeTextColor: tintColor,
UITextAttributeTextShadowOffset: [NSValue valueWithCGSize:CGSizeZero],
UITextAttributeTextShadowColor: [UIColor clearColor]};
[self setTitleTextAttributes:attributes forState:UIControlStateNormal];
[self setTitleTextAttributes:attributes forState:UIControlStateDisabled];
[self setTitleTextAttributes:attributes forState:UIControlStateReserved];
[self setTitleTextAttributes:attributes forState:UIControlStateApplication];
NSDictionary *highlightedAttributes = @{UITextAttributeFont: font,
UITextAttributeTextColor: highlightedTextColor,
UITextAttributeTextShadowOffset: [NSValue valueWithCGSize:CGSizeZero],
UITextAttributeTextShadowColor: [UIColor clearColor]};
[self setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted];
[self setTitleTextAttributes:highlightedAttributes forState:UIControlStateSelected];
[self setBackgroundImage:selectedBackgroundImage
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:normalBackgroundImage
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
[self setDividerImage:[UIImage imageWithColor:tintColor]
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment