UICustomSwitch - color it, change labels

sample code that might be beneficial to others...

UICustomSwitch - color it, change labels

Postby HardyMacia » Thu Oct 29, 2009 4:07 pm

After looking for a day at how to change the blue of the UISwitch to a different color so it can match my theme in PocketMoney I ended up writing my own. This will let you change the tintColor and the labels. If you want to modify other aspect so fit to change the label to images you'll have to modify the .png files.

This code can be freely used in any kind of project as long as the copyright information stays intact. If you improve on the code then please share it back to me so I can integrate it in with my code.

- Hardy
Attachments
UICustomSwitch.zip
UICustomSwitch .h/.m and project test file
(29.83 KiB) Downloaded 786 times
Hardy Macia
Catamount Software •  support@catamount.com
Follow me on Twitter: @HardyMacia or @CatamountSW
User avatar
HardyMacia
Site Admin
 
Posts: 3575
Joined: Thu May 04, 2006 3:07 pm
Location: Canterbury, NH

Re: UICustomSwitch - color it, change labels

Postby HardyMacia » Fri Oct 30, 2009 10:44 am

Update to code is attached:

• Fixed bugs with valueChanged notification
• Set the backgroundColor to clear for whole switch.
Attachments
UICustomSwitch.zip
(29.98 KiB) Downloaded 414 times
Hardy Macia
Catamount Software •  support@catamount.com
Follow me on Twitter: @HardyMacia or @CatamountSW
User avatar
HardyMacia
Site Admin
 
Posts: 3575
Joined: Thu May 04, 2006 3:07 pm
Location: Canterbury, NH

Re: UICustomSwitch - color it, change labels

Postby HardyMacia » Sat Oct 31, 2009 12:44 pm

Update to code is attached:

• Fixed the rounded corners not drawing correctly. Mask is now applied before using the blending so the alpha channel is honored.
Attachments
UICustomSwitch.zip
UICustomSwitch .h/.m and project test file
(30.21 KiB) Downloaded 2822 times
Hardy Macia
Catamount Software •  support@catamount.com
Follow me on Twitter: @HardyMacia or @CatamountSW
User avatar
HardyMacia
Site Admin
 
Posts: 3575
Joined: Thu May 04, 2006 3:07 pm
Location: Canterbury, NH

Re: UICustomSwitch - color it, change labels

Postby HardyMacia » Thu Jan 28, 2010 8:46 am

From Renton
E-mail : e.fortunati@yourworldinyourhands.eu

It was great!!!
just added

- (void)scaleSwitch:(CGSize)newSize {
self.transform = CGAffineTransformMakeScale(newSize.width,newSize.height);
}

to allow rescaling!

Thanks for your customSwitch!
Hardy Macia
Catamount Software •  support@catamount.com
Follow me on Twitter: @HardyMacia or @CatamountSW
User avatar
HardyMacia
Site Admin
 
Posts: 3575
Joined: Thu May 04, 2006 3:07 pm
Location: Canterbury, NH

Re: UICustomSwitch - color it, change labels

Postby HardyMacia » Thu Jan 28, 2010 8:56 am

Two updates to the custom switch code from other developers. Thanks Simran and Renton.

Thumb.png transparency fixed.
Scale method added for resizing.
Attachments
UICustomSwitch.zip
Thumb.png transparency fixed. Scale method added for resizing..
(15.65 KiB) Downloaded 15956 times
Hardy Macia
Catamount Software •  support@catamount.com
Follow me on Twitter: @HardyMacia or @CatamountSW
User avatar
HardyMacia
Site Admin
 
Posts: 3575
Joined: Thu May 04, 2006 3:07 pm
Location: Canterbury, NH

Re: UICustomSwitch - color it, change labels

Postby HardyMacia » Wed Feb 03, 2010 10:05 am

From dejo in comments from blog post...

Here's how I enhanced it: I needed to be able to tint the off color as well (for a M/blue vs. F/pink gender switch). So I added the following method, with a supporting property as well (not shown):

Code: Select all
-(void)setOffTintColor:(UIColor*)color
{
   if (color != offTintColor)
   {
      [offTintColor release];
      offTintColor = [color retain];
      
      [self setMaximumTrackImage:[self image:[UIImage imageNamed:@"switchOffPlain.png"] tintedWithColor:offTintColor] forState:UIControlStateNormal];
   }
   
}
Hardy Macia
Catamount Software •  support@catamount.com
Follow me on Twitter: @HardyMacia or @CatamountSW
User avatar
HardyMacia
Site Admin
 
Posts: 3575
Joined: Thu May 04, 2006 3:07 pm
Location: Canterbury, NH

Re: UICustomSwitch - color it, change labels

Postby alextrob » Thu Mar 25, 2010 8:47 am

Thanks for releasing this code; it's been pretty useful.
I've done a few little things to to make it look & behave a bit more like the standard UISwitch.

1. In the awakeFromNib method, I've added this to make the thumbImage adjust its state when it's pressed:
Code: Select all
[self setThumbImage:[UIImage imageNamed:@"switchThumbSel.png"] forState:UIControlStateHighlighted];
I've attached an example of the switchThumbSel.png based on the switchThumb.png that came with the code.

2. If you look closely at the standard UISwitch, it's got a dark shadow above the 'ON' text and a highlight below the 'OFF' text. So if you want to duplicate that for your custom switch...

Put this after self.leftLabel = [[UILabel alloc] init]; where the properties of leftLabel are being set:
Code: Select all
[self.leftLabel setShadowColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
[self.leftLabel setShadowOffset:CGSizeMake(0, -1)];

And this after self.rightLabel = [[UILabel alloc] init];
Code: Select all
[self.rightLabel setShadowColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5]];
[self.leftLabel setShadowOffset:CGSizeMake(0, 1)];
Attachments
switchThumbSel.png
switchThumbSel.png (429 Bytes) Viewed 8398 times
alextrob
 
Posts: 1
Joined: Thu Mar 25, 2010 7:58 am

Re: UICustomSwitch - color it, change labels

Postby HardyMacia » Sun Jul 18, 2010 5:43 pm

FYI, as pointed out by @PeyloW twit Apple doesn't want us using their class prefixes for custom controls. Better if you called it CSCustomSwitch or maybe CSCustom_UISwitch.
Hardy Macia
Catamount Software •  support@catamount.com
Follow me on Twitter: @HardyMacia or @CatamountSW
User avatar
HardyMacia
Site Admin
 
Posts: 3575
Joined: Thu May 04, 2006 3:07 pm
Location: Canterbury, NH

Re: UICustomSwitch - color it, change labels

Postby Srhone » Sun Mar 11, 2012 8:29 pm

hate to sound like a total noob here, but is it possible to get an example of how to use this? Been hitting my head against a wall trying to figure it out.

Thanks
Srhone
 
Posts: 1
Joined: Sun Mar 11, 2012 8:26 pm

Re: UICustomSwitch - color it, change labels

Postby HardyMacia » Mon Dec 03, 2012 7:11 pm

I stopped using the UICustomSwitch since Apple supporting tinting as of iOS 5.0 and that was my main use of the custom switch class, but Doug has updated it for iOS 6 and fixed a few issues in it.

I used your UICustomSwitch code in a project I am working on. I took your source and refactored it to a HMCustomSwitch class, updated the graphics and took care of a couple deprecations in iOS6. I figured I would update it a bit for you so I could give back a bit.

Thanks for putting great code out for others to learn from and use!

Thanks
Doug Drury
Attachments
HMCustomSwitch.zip
(48.22 KiB) Downloaded 113 times
Hardy Macia
Catamount Software •  support@catamount.com
Follow me on Twitter: @HardyMacia or @CatamountSW
User avatar
HardyMacia
Site Admin
 
Posts: 3575
Joined: Thu May 04, 2006 3:07 pm
Location: Canterbury, NH


Return to Source Code Snippets

Who is online

Users browsing this forum: No registered users