Skip to content

Instantly share code, notes, and snippets.

@happyrobots
Created January 2, 2014 11:16

Revisions

  1. Nia Mutiara revised this gist Jan 2, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UISegmentedControl+iOS7Flat.m
    Original file line number Diff line number Diff line change
    @@ -47,7 +47,7 @@ + (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size andRoundSize:(CGF



    @implementation UISegmentedControl (THCompatibility)
    @implementation UISegmentedControl (iOS7Flat)

    - (void)flattenUIWithFont:(UIFont *)font tintColor:(UIColor *)tintColor highlightedTextColor:(UIColor *)highlightedTextColor
    {
  2. Nia Mutiara created this gist Jan 2, 2014.
    3 changes: 3 additions & 0 deletions UISegmentedControl+iOS7Flat.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    @interface UISegmentedControl (iOS7Flat)
    - (void)flattenUIWithFont:(UIFont *)font tintColor:(UIColor *)tintColor highlightedTextColor:(UIColor *)highlightedTextColor;
    @end
    95 changes: 95 additions & 0 deletions UISegmentedControl+iOS7Flat.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    #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 (THCompatibility)

    - (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