Last active
August 11, 2020 03:33
-
-
Save LacertosusRepo/e0df321b96c1affbc446b17d8ea32391 to your computer and use it in GitHub Desktop.
Custom color switch cell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//then in your .plist file you need to add some properties to your PSSwitchCell and don't forget to add the XXXSwitchCell.m file to your makefile | |
<dict> | |
<key>cell</key> | |
<string>PSSwitchCell</string> | |
<key>cellClass</key> #Add the key cellClass and set it to your custom switch subclass | |
<string>XXXSwitchCell</string> | |
<key>switchColor</key> #Add the key switchColor and set your wanted hex color, defaults to #FFFFFF | |
<string>#800080</string> | |
<key>switchColorAlpha</key> #Add the key switchColorAlpha and set your wanted alpha for the color, defaults to 1.0 | |
<real>1.0</real> | |
<key>default</key> | |
<false/> | |
<key>defaults</key> | |
<string>com.company.prefs</string> | |
<key>key</key> | |
<string>switchKey</string> | |
<key>label</key> | |
<string>This is a switch</string> | |
<key>PostNotification</key> | |
<string>com.company.prefs/ReloadPrefs</string> | |
</dict> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Preferences/PSSwitchTableCell.h> | |
#import <Preferences/PSSpecifier.h> | |
//create a subclass of PSSwitchTableCell, it is recommended to change the XXX prefix to be the same as your preferences project prefix | |
@interface XXXSwitchCell : PSSwitchTableCell | |
-(UIColor *)colorFromHex:(NSString *)hex withAlpha:(CGFloat)alpha; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation XXXSwitchCell | |
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)identifier specifier:(PSSpecifier *)specifier { | |
self = [super initWithStyle:style reuseIdentifier:identifier specifier:specifier]; | |
//set the on tint color of the switch using the properties from the specifier | |
if(self) { | |
NSString *hexString = ([specifier propertyForKey:@"switchColor"]) ?: @"#FFFFFF"; | |
float hexAlpha = ([[specifier propertyForKey:@"switchColorAlpha"] floatValue]) ?: 1.0; | |
((UISwitch *)self.control).onTintColor = [self colorFromHex:hexString withAlpha:hexAlpha]; | |
} | |
return self; | |
} | |
//add method to convert hex to UIColor, notice we added this method to the cell's interface | |
//StackOverFlow - https://stackoverflow.com/a/12397366 | |
-(UIColor *)colorFromHex:(NSString *)hex withAlpha:(CGFloat)alpha { | |
unsigned rgbValue = 0; | |
NSScanner *scanner = [NSScanner scannerWithString:hex]; | |
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]]; | |
[scanner scanHexInt:&rgbValue]; | |
return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0 green:((float)((rgbValue & 0x00FF00) >> 8)) / 255.0 blue:((float)((rgbValue & 0x0000FF) >> 0)) / 255.0 alpha:alpha]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment