From ffb3ad5072cb0dbbd76a4b19d1b4d3eabf00a07b Mon Sep 17 00:00:00 2001 From: David Date: Sun, 10 Apr 2016 12:04:28 -0700 Subject: [PATCH] Added example for using with Swift. Made APIs more Swift friendly. --- CHANGELOG.md | 5 + DLRadioButton.podspec.json | 4 +- DLRadioButton.xcodeproj/project.pbxproj | 4 +- DLRadioButton/DLRadioButton.h | 46 +-- Example/DLRadioButton.framework/DLRadioButton | Bin 281248 -> 281248 bytes .../Headers/DLRadioButton.h | 46 +-- .../_CodeSignature/CodeResources | 4 +- .../project.pbxproj | 10 +- .../DLDemoViewController.h | 2 - .../DLDemoViewController.m | 75 ++-- .../DemoViewController.swift | 67 ++++ Example/DLRadioButtonExample/Main.storyboard | 339 +++++++++++++++++- README.md | 6 +- 13 files changed, 510 insertions(+), 98 deletions(-) create mode 100644 Example/DLRadioButtonExample/DemoViewController.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 61b9c75..6854421 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. --- +## [1.4.8](https://github.com/DavydLiu/DLRadioButton/releases/tag/1.4.8) (04/10/2016) +* Added example for using with Swift. +* Made APIs more Swift friendly. + + ## [1.4.7](https://github.com/DavydLiu/DLRadioButton/releases/tag/1.4.7) (04/02/2016) * Added support for RTL interface layout. * Updated API comments. diff --git a/DLRadioButton.podspec.json b/DLRadioButton.podspec.json index b5d55f8..ef7f097 100644 --- a/DLRadioButton.podspec.json +++ b/DLRadioButton.podspec.json @@ -1,6 +1,6 @@ { "name": "DLRadioButton", - "version": "1.4.7", + "version": "1.4.8", "summary": "A highly customizable Radio Button for iOS", "description": "A highly customizable Radio Button for iOS.\n\n* Buttons are drew by UIBezierPath, customize it however you want.\n* You can also use pictures to indicate buttons' selection state.\n", "homepage": "https://github.com/DavydLiu/DLRadioButton", @@ -17,7 +17,7 @@ }, "source": { "git": "https://github.com/DavydLiu/DLRadioButton.git", - "tag": "1.4.7" + "tag": "1.4.8" }, "source_files": "DLRadioButton/**/*.{h,m}", "exclude_files": "Classes/Exclude", diff --git a/DLRadioButton.xcodeproj/project.pbxproj b/DLRadioButton.xcodeproj/project.pbxproj index 26d2db2..e6dc458 100644 --- a/DLRadioButton.xcodeproj/project.pbxproj +++ b/DLRadioButton.xcodeproj/project.pbxproj @@ -300,7 +300,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 6.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -342,7 +342,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 6.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; TARGETED_DEVICE_FAMILY = "1,2"; diff --git a/DLRadioButton/DLRadioButton.h b/DLRadioButton/DLRadioButton.h index dcc1c28..1a983f4 100644 --- a/DLRadioButton/DLRadioButton.h +++ b/DLRadioButton/DLRadioButton.h @@ -6,11 +6,35 @@ IB_DESIGNABLE @interface DLRadioButton : UIButton +#pragma mark - Access buttons + +/** + * Finds out selected button in same group. + * + * @return Selected button. + */ +- (nullable DLRadioButton *)selectedButton; + +/** + * Finds out selected buttons in same group, use it only if multiple selection is enabled. + * + * @return Selected buttons. + */ +- (nonnull NSArray *)selectedButtons; + /** * Container for holding other buttons in same group. */ -@property (nonatomic) IBOutletCollection(DLRadioButton) NSArray *otherButtons; +@property (nonnull, nonatomic) IBOutletCollection(DLRadioButton) NSArray *otherButtons; + +/** + * Clears selection for other buttons in in same group. + */ +- (void)deselectOtherButtons; +#pragma mark - Customization + +NS_ASSUME_NONNULL_BEGIN /** * Size of icon, default is kDefaulIconSize. */ @@ -66,24 +90,6 @@ IB_DESIGNABLE * Whether enable multiple selection, default is NO. */ @property (nonatomic, getter = isMultipleSelectionEnabled) BOOL multipleSelectionEnabled; - -/** - * Finds out selected button in same group. - * - * @return Selected button. - */ -- (DLRadioButton *)selectedButton; - -/** - * Finds out selected buttons in same group, use it only if multiple selection is enabled. - * - * @return Selected buttons. - */ -- (NSArray *)selectedButtons; - -/** - * Clears selection for other buttons in in same group. - */ -- (void)deselectOtherButtons; +NS_ASSUME_NONNULL_END @end diff --git a/Example/DLRadioButton.framework/DLRadioButton b/Example/DLRadioButton.framework/DLRadioButton index 29712cca07ae0ed2cb8c1c832bc1bd507b1e2446..dfa059a0a2b1ea745361ffd2cd3587be267d629b 100755 GIT binary patch delta 911 zcmZ25RdB&n!3`O+n5VUIZO)$cmK(%qj$XDsdKu%_6y~XIT-$@WnLctbPXV&rMVK;U zL^?(H97$(!eyAq@z2w6B`(ph^PN{4^QoxkKsIl_zjX=Zxk~^R0Z9RX^fARl0FX#BZ zb(0M4zw7;2tet z_Dwc7;5L<-TAh7>sjc_Kf$t#>=1xtLmHexI%)Hs>`+MH(5N3m3b8USs<=^QOUiW$i zBt_?$?|4@kBWeEhjOXgE!*P0Bw%hKlQwwt{J9;wr`v`S|#?UQ2GSjF~D+Sror6z=~g@q6!OWB((%my-gWa$Cz!T~>{ITVRx~ zda!fW?40v9Wu-T@{%_nk(dzww?v};IyU%W_lTT6($*xbTyu(u=Fw2Hf->%+vN6~4f z_^;hx8n<8MVyv=mJHWK<0J8)aIPRoRF$1F%6nEU$n3wwDh&vS)VB9TnzZ)Ct^)ltz zuD>(UoX;mtk2!6Hr|7XAndcohY!6Xmxri_BvTvj>JyffEj!|@$ zzMd1~A8%2a)svQm7yXjVc@iM?@g0`9vlQfR3RK_wLD%>|$=k}_M|=X}E8JwIABlD+ zJMu$n z7fv6u?lW7e?%c5OM(@Rx+lEiVVi|4ZC!EfhEtuIdEpa1TqTKVC zv1gT?-IJ?|O;y{Q&a`wjRFz-ZRWr%{+Ksmy$}=Zd$IHjKT1#^YN3hLqJ92i_exsr% z=cXU=KmTdziJ-R=9(jsB`Tw{=>uiq58twOo$_y?qmPt`c@CnSXDU(?9QfhDYQa#^k tuO&Idu5@-)@h&$^eY10m--_H-yC+?C&(gG)pt#c$0szUOmM{PS delta 911 zcmZ25RdB&n!3`O+m|KJyHfPUz%MD^QM=#qRy^Qf|3UjkC!}efqrjHz8mb(a3W{gO7 z?E0A^1-h3#>O%Jw-B`8Ot)xAF`;h{s3`ULm|J^%E)<+4u7FR5fbh#L+5%9+Nr<|wS z^ru-re0I%Jb(g+;WM!kA@b-`*rl$h+21bS!CT2z!Q3m>KoLX%jZQpqrIav&qSQHpe z-4(s!XC)wU-s$867uKJ3wq`X(Ta=dfMb!RXGDFYMfETWTk&%^wrHPRtFwxC*dTNVu zzwK1nkSz5=>lrhv6#p8pR{C1<+<9Avuq|)aRPLi8g-qAk3MAj3?qkiVKNYw1YPrPO z6&n_vD)dudZ@HT1qU8C#xr_fVc)RM%4}X8gquUOAU+9=}WJ1ILxt-51+Fxv)a&~ru zQQzl)a)leSru;i(=WceQ@%WFrh=mcs-+jxI&3>N>4i-Q6@bm5*)sD&k3cG@3u78j| z|Gz{qe3{edfO!^cvFiJtO$bF4@sUMEGQ(*zdUH-?~zk7Ve9^Y}S ze5fsQ(C+ke@r*_L8Itcdd`*11Fm&I6r~B^f?)IwQ9-_u_5ntRLyVCz#tW244@3qBp z`C@;SEbArg4bJNOUo4lA`E`V0C6>4oGZC5g;268hHIsxtJ->cru1GKXEdH>#{zCbr z(;eSZly|M@dLrn|I-!15e0cxTpev^N^&0#C{HS)^%Fw#Oq3!ACYa0p!K01ii&A7F4 z?Ni^5(m$km+*Vdn=cx_-%7v7C*~*}wqwV~_zAq%_uLbi tayR@MAZ *)selectedButtons; + /** * Container for holding other buttons in same group. */ -@property (nonatomic) IBOutletCollection(DLRadioButton) NSArray *otherButtons; +@property (nonnull, nonatomic) IBOutletCollection(DLRadioButton) NSArray *otherButtons; + +/** + * Clears selection for other buttons in in same group. + */ +- (void)deselectOtherButtons; +#pragma mark - Customization + +NS_ASSUME_NONNULL_BEGIN /** * Size of icon, default is kDefaulIconSize. */ @@ -66,24 +90,6 @@ IB_DESIGNABLE * Whether enable multiple selection, default is NO. */ @property (nonatomic, getter = isMultipleSelectionEnabled) BOOL multipleSelectionEnabled; - -/** - * Finds out selected button in same group. - * - * @return Selected button. - */ -- (DLRadioButton *)selectedButton; - -/** - * Finds out selected buttons in same group, use it only if multiple selection is enabled. - * - * @return Selected buttons. - */ -- (NSArray *)selectedButtons; - -/** - * Clears selection for other buttons in in same group. - */ -- (void)deselectOtherButtons; +NS_ASSUME_NONNULL_END @end diff --git a/Example/DLRadioButton.framework/_CodeSignature/CodeResources b/Example/DLRadioButton.framework/_CodeSignature/CodeResources index b12b316..373e314 100644 --- a/Example/DLRadioButton.framework/_CodeSignature/CodeResources +++ b/Example/DLRadioButton.framework/_CodeSignature/CodeResources @@ -6,7 +6,7 @@ Headers/DLRadioButton.h - AUTpUW+3DpeB/74Awfr3kVxsj5U= + AxrEoeOQUM+yJB38xuZlF8jcNuw= Info.plist @@ -21,7 +21,7 @@ Headers/DLRadioButton.h - AUTpUW+3DpeB/74Awfr3kVxsj5U= + AxrEoeOQUM+yJB38xuZlF8jcNuw= Modules/module.modulemap diff --git a/Example/DLRadioButtonExample.xcodeproj/project.pbxproj b/Example/DLRadioButtonExample.xcodeproj/project.pbxproj index b073b56..a66dd4b 100644 --- a/Example/DLRadioButtonExample.xcodeproj/project.pbxproj +++ b/Example/DLRadioButtonExample.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 63B875781CB047AB00AE8E98 /* DLRadioButton.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 63B875771CB047AB00AE8E98 /* DLRadioButton.framework */; }; 63B875791CB0481900AE8E98 /* DLRadioButton.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 63B875771CB047AB00AE8E98 /* DLRadioButton.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + 63B8757B1CBAB95800AE8E98 /* DemoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63B8757A1CBAB95800AE8E98 /* DemoViewController.swift */; }; 63C933161B48EB1C009DD715 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 63C933151B48EB1C009DD715 /* Images.xcassets */; }; D1BE3A0319A7D263004BD3F5 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D1BE3A0219A7D263004BD3F5 /* Foundation.framework */; }; D1BE3A0519A7D263004BD3F5 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D1BE3A0419A7D263004BD3F5 /* CoreGraphics.framework */; }; @@ -36,6 +37,7 @@ /* Begin PBXFileReference section */ 63B875771CB047AB00AE8E98 /* DLRadioButton.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = DLRadioButton.framework; sourceTree = ""; }; + 63B8757A1CBAB95800AE8E98 /* DemoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DemoViewController.swift; sourceTree = ""; }; 63C933151B48EB1C009DD715 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; D1BE39FF19A7D263004BD3F5 /* DLRadioButtonExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DLRadioButtonExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; D1BE3A0219A7D263004BD3F5 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; @@ -101,10 +103,11 @@ D1BE3A1119A7D263004BD3F5 /* DLAppDelegate.h */, D1BE3A1219A7D263004BD3F5 /* DLAppDelegate.m */, 63C933151B48EB1C009DD715 /* Images.xcassets */, - D1BE3A0919A7D263004BD3F5 /* Supporting Files */, D1BE3A3519AB683D004BD3F5 /* Main.storyboard */, D1BE3A3719AB7000004BD3F5 /* DLDemoViewController.h */, D1BE3A3819AB7000004BD3F5 /* DLDemoViewController.m */, + 63B8757A1CBAB95800AE8E98 /* DemoViewController.swift */, + D1BE3A0919A7D263004BD3F5 /* Supporting Files */, ); path = DLRadioButtonExample; sourceTree = ""; @@ -148,6 +151,7 @@ isa = PBXProject; attributes = { CLASSPREFIX = DL; + LastSwiftUpdateCheck = 0730; LastUpgradeCheck = 0700; ORGANIZATIONNAME = "Xingruo Liu"; TargetAttributes = { @@ -198,6 +202,7 @@ D1BE3A1319A7D263004BD3F5 /* DLAppDelegate.m in Sources */, D1BE3A3919AB7000004BD3F5 /* DLDemoViewController.m in Sources */, D1BE3A0F19A7D263004BD3F5 /* main.m in Sources */, + 63B8757B1CBAB95800AE8E98 /* DemoViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -292,6 +297,7 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -307,6 +313,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.liuxingruo.$(PRODUCT_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; WRAPPER_EXTENSION = app; }; name = Debug; @@ -315,6 +322,7 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; diff --git a/Example/DLRadioButtonExample/DLDemoViewController.h b/Example/DLRadioButtonExample/DLDemoViewController.h index 0969752..84828a8 100644 --- a/Example/DLRadioButtonExample/DLDemoViewController.h +++ b/Example/DLRadioButtonExample/DLDemoViewController.h @@ -3,6 +3,4 @@ @interface DLDemoViewController : UIViewController -@property (weak, nonatomic) IBOutlet DLRadioButton *waterButton; - @end diff --git a/Example/DLRadioButtonExample/DLDemoViewController.m b/Example/DLRadioButtonExample/DLDemoViewController.m index c0e5921..17e86aa 100644 --- a/Example/DLRadioButtonExample/DLDemoViewController.m +++ b/Example/DLRadioButtonExample/DLDemoViewController.m @@ -1,18 +1,12 @@ #import "DLDemoViewController.h" -@implementation DLDemoViewController +@interface DLDemoViewController () -#pragma mark - Helpers +@property (weak, nonatomic) IBOutlet DLRadioButton *waterButton; -- (IBAction)logSelectedButton:(DLRadioButton *)radiobutton { - if (radiobutton.isMultipleSelectionEnabled) { - for (DLRadioButton *button in radiobutton.selectedButtons) { - NSLog(@"%@ is selected.\n", button.titleLabel.text); - } - } else { - NSLog(@"%@ is selected.\n", radiobutton.selectedButton.titleLabel.text); - } -} +@end + +@implementation DLDemoViewController #pragma mark - UIViewController @@ -22,31 +16,23 @@ - (void)viewDidLoad { // enable multiple selection for water, beer and wine buttons. self.waterButton.multipleSelectionEnabled = YES; - // programmatically add buttons + // programmatically add button // first button - DLRadioButton *firstRadioButton = [[DLRadioButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 131, 350, 262, 17)]; - firstRadioButton.titleLabel.font = [UIFont systemFontOfSize:14]; - [firstRadioButton setTitle:@"Red Button" forState:UIControlStateNormal]; - [firstRadioButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; - firstRadioButton.iconColor = [UIColor redColor]; - firstRadioButton.indicatorColor = [UIColor redColor]; - firstRadioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; - [firstRadioButton addTarget:self action:@selector(logSelectedButton:) forControlEvents:UIControlEventTouchUpInside]; - [self.view addSubview:firstRadioButton]; + CGRect frame = CGRectMake(self.view.frame.size.width / 2 - 131, 350, 262, 17); + DLRadioButton *firstRadioButton = [self createRadioButtonWithFrame:frame + Title:@"Red Button" + Color:[UIColor redColor]]; // other buttons NSArray *colorNames = @[@"Brown", @"Orange", @"Green", @"Blue", @"Purple"]; - NSArray *buttonColors = @[[UIColor brownColor], [UIColor orangeColor], [UIColor greenColor], [UIColor blueColor], [UIColor purpleColor]]; + NSArray *colors = @[[UIColor brownColor], [UIColor orangeColor], [UIColor greenColor], [UIColor blueColor], [UIColor purpleColor]]; NSInteger i = 0; NSMutableArray *otherButtons = [NSMutableArray new]; - for (UIColor *buttonColor in buttonColors) { - // customize button - DLRadioButton *radioButton = [[DLRadioButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 131, 380 + 30 * i, 262, 17)]; - radioButton.titleLabel.font = [UIFont systemFontOfSize:14]; - [radioButton setTitle:[colorNames[i] stringByAppendingString:@" Button"] forState:UIControlStateNormal]; - [radioButton setTitleColor:buttonColor forState:UIControlStateNormal]; - radioButton.iconColor = buttonColor; - radioButton.indicatorColor = buttonColor; + for (UIColor *color in colors) { + CGRect frame = CGRectMake(self.view.frame.size.width / 2 - 131, 380 + 30 * i, 262, 17); + DLRadioButton *radioButton = [self createRadioButtonWithFrame:frame + Title:[colorNames[i] stringByAppendingString:@" Button"] + Color:color]; if (i % 2 == 0) { radioButton.iconSquare = YES; } @@ -54,20 +40,39 @@ - (void)viewDidLoad { // put icon on the right side radioButton.iconOnRight = YES; radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; - } else { - radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; } [otherButtons addObject:radioButton]; - [radioButton addTarget:self action:@selector(logSelectedButton:) forControlEvents:UIControlEventTouchUpInside]; - [self.view addSubview:radioButton]; i++; } firstRadioButton.otherButtons = otherButtons; } -- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { +#pragma mark - Helper + +- (DLRadioButton *)createRadioButtonWithFrame:(CGRect) frame Title:(NSString *)title Color:(UIColor *)color +{ + DLRadioButton *radioButton = [[DLRadioButton alloc] initWithFrame:frame]; + radioButton.titleLabel.font = [UIFont systemFontOfSize:14]; + [radioButton setTitle:title forState:UIControlStateNormal]; + [radioButton setTitleColor:color forState:UIControlStateNormal]; + radioButton.iconColor = color; + radioButton.indicatorColor = color; + radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; + [radioButton addTarget:self action:@selector(logSelectedButton:) forControlEvents:UIControlEventTouchUpInside]; + [self.view addSubview:radioButton]; + return radioButton; +} + +- (IBAction)logSelectedButton:(DLRadioButton *)radioButton { + if (radioButton.isMultipleSelectionEnabled) { + for (DLRadioButton *button in radioButton.selectedButtons) { + NSLog(@"%@ is selected.\n", button.titleLabel.text); + } + } else { + NSLog(@"%@ is selected.\n", radioButton.selectedButton.titleLabel.text); + } } @end diff --git a/Example/DLRadioButtonExample/DemoViewController.swift b/Example/DLRadioButtonExample/DemoViewController.swift new file mode 100644 index 0000000..3624f72 --- /dev/null +++ b/Example/DLRadioButtonExample/DemoViewController.swift @@ -0,0 +1,67 @@ +import UIKit +import DLRadioButton + +class DemoViewController : UIViewController { + @IBOutlet var waterButton : DLRadioButton!; + + override func viewDidLoad() { + super.viewDidLoad(); + + // enable multiple selection for water, beer and wine buttons. + self.waterButton.multipleSelectionEnabled = true; + + // programmatically add buttons + // first button + let frame = CGRectMake(self.view.frame.size.width / 2 - 131, 350, 262, 17); + let firstRadioButton = createRadioButton(frame, title: "Red Button", color: UIColor.redColor()); + + //other buttons + let colorNames = ["Brown", "Orange", "Green", "Blue", "Purple"]; + let colors = [UIColor.brownColor(), UIColor.orangeColor(), UIColor.greenColor(), UIColor.blueColor(), UIColor.purpleColor()]; + var i = 0; + var otherButtons : [DLRadioButton] = []; + for color in colors { + let frame = CGRectMake(self.view.frame.size.width / 2 - 131, 380 + 30 * CGFloat(i), 262, 17); + let radioButton = createRadioButton(frame, title: colorNames[i] + " Button", color: color); + if (i % 2 == 0) { + radioButton.iconSquare = true; + } + if (i > 1) { + // put icon on the right side + radioButton.iconOnRight = true; + radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Right; + } + otherButtons.append(radioButton); + i += 1; + } + + firstRadioButton.otherButtons = otherButtons; + } + + // MARK: Helper + + private func createRadioButton(frame : CGRect, title : String, color : UIColor) -> DLRadioButton { + let radioButton = DLRadioButton(frame: frame); + radioButton.titleLabel!.font = UIFont.systemFontOfSize(14); + radioButton.setTitle(title, forState: UIControlState.Normal); + radioButton.setTitleColor(color, forState: UIControlState.Normal); + radioButton.iconColor = color; + radioButton.indicatorColor = color; + radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left; + radioButton.addTarget(self, action: #selector(DemoViewController.logSelectedButton), forControlEvents: UIControlEvents.TouchUpInside); + self.view.addSubview(radioButton); + + return radioButton; + } + + @objc @IBAction private func logSelectedButton(radioButton : DLRadioButton) { + if (radioButton.multipleSelectionEnabled) { + for button in radioButton.selectedButtons() { + print(String(format: "%@ is selected.\n", button.titleLabel!.text!)); + } + } else { + print(String(format: "%@ is selected.\n", radioButton.selectedButton()!.titleLabel!.text!)); + } + } +} + diff --git a/Example/DLRadioButtonExample/Main.storyboard b/Example/DLRadioButtonExample/Main.storyboard index 9027c17..06d6328 100644 --- a/Example/DLRadioButtonExample/Main.storyboard +++ b/Example/DLRadioButtonExample/Main.storyboard @@ -1,11 +1,29 @@ - + - + - + + + + + + + + + + + + + + + + + + + @@ -23,7 +41,7 @@ - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/README.md b/README.md index 5412d0c..c543a69 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,10 @@ [![Platform](https://img.shields.io/cocoapods/p/DLRadioButton.svg?style=flat)](http://cocoadocs.org/docsets/DLRadioButton) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) +`DLRadioButton` is an easy to use and highly customizable radio buttons control for iOS. It's a subclass of `UIButton`, and works smoothly with both `Objective-C` and `Swift`. + +Since release, it has became the most popular radio buttons control for iOS🎉. Thank you for great feedbacks and making it better for all of us👏. + ## Preview ![screenshot1](Images/DLRadioButton_screenshot1.png) @@ -48,7 +52,7 @@ ARC, iOS 6.0 ## Author -David Liu. For help or any qeustions, feel free to [open an issue](https://github.com/DavydLiu/DLRadioButton/issues/new). +David Liu. For help or any questions, feel free to [open an issue](https://github.com/DavydLiu/DLRadioButton/issues/new). ## License