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 167 times
Hardy Macia
Catamount Software
Follow me on Twitter: http://www.twitter.com/hardymacia/
User avatar
HardyMacia
Site Admin
 
Posts: 2479
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 117 times
Hardy Macia
Catamount Software
Follow me on Twitter: http://www.twitter.com/hardymacia/
User avatar
HardyMacia
Site Admin
 
Posts: 2479
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 368 times
Hardy Macia
Catamount Software
Follow me on Twitter: http://www.twitter.com/hardymacia/
User avatar
HardyMacia
Site Admin
 
Posts: 2479
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
Follow me on Twitter: http://www.twitter.com/hardymacia/
User avatar
HardyMacia
Site Admin
 
Posts: 2479
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 1151 times
Hardy Macia
Catamount Software
Follow me on Twitter: http://www.twitter.com/hardymacia/
User avatar
HardyMacia
Site Admin
 
Posts: 2479
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
Follow me on Twitter: http://www.twitter.com/hardymacia/
User avatar
HardyMacia
Site Admin
 
Posts: 2479
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 912 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
Follow me on Twitter: http://www.twitter.com/hardymacia/
User avatar
HardyMacia
Site Admin
 
Posts: 2479
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