Skip to content

Commit d354aed

Browse files
André Felipe Santosdogo
authored andcommitted
Implement SCLSwitchView
Add switch alert example to demo app Update doc
1 parent 3367dc0 commit d354aed

11 files changed

Lines changed: 271 additions & 26 deletions

File tree

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ alert.attributedFormatBlock = ^NSAttributedString* (NSString *value)
123123
124124
[alert showSuccess:self title:@"Button View" subTitle:@"Attributed string operation successfully completed." closeButtonTitle:@"Done" duration:0.0f];
125125
```
126+
126127
###Add a text field
127128
```Objective-C
128129
SCLAlertView *alert = [[SCLAlertView alloc] init];
@@ -142,7 +143,19 @@ SCLAlertView *alert = [[SCLAlertView alloc] init];
142143
143144
[alert showWaiting:self title:@"Waiting..." subTitle:@"Blah de blah de blah, blah. Blah de blah de" closeButtonTitle:nil duration:5.0f];
144145
```
145-
146+
###Add a switch button
147+
```Objective-C
148+
SCLAlertView *alert = [[SCLAlertView alloc] init];
149+
150+
SCLSwitchView *switchView = [alert addSwitchViewWithLabel:@"Don't show again".uppercaseString];
151+
switchView.tintColor = [UIColor brownColor];
152+
153+
[alert addButton:@"Done" actionBlock:^(void) {
154+
NSLog(@"Show again? %@", switchView.isSelected ? @"-No": @"-Yes");
155+
}];
156+
157+
[alert showCustom:self image:[UIImage imageNamed:@"switch"] color:[UIColor brownColor] title:kInfoTitle subTitle:kSubtitle closeButtonTitle:nil duration:0.0f];
158+
```
146159
147160
###SCLAlertView properties
148161
```Objective-C

SCLAlertView.xcodeproj/project.pbxproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
/* Begin PBXBuildFile section */
1010
572BB1471B8383B3002DEE38 /* SCLTimerDisplay.m in Sources */ = {isa = PBXBuildFile; fileRef = 572BB1461B8383B3002DEE38 /* SCLTimerDisplay.m */; };
11-
DD24453C1BAC383600892117 /* SCLTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = DD24453B1BAC383600892117 /* SCLTextView.m */; settings = {ASSET_TAGS = (); }; };
11+
CF6FB1241C5926FC009715F3 /* SCLSwitchView.m in Sources */ = {isa = PBXBuildFile; fileRef = CF6FB1231C5926FC009715F3 /* SCLSwitchView.m */; };
12+
DD24453C1BAC383600892117 /* SCLTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = DD24453B1BAC383600892117 /* SCLTextView.m */; };
1213
DD2E172A19FA84B800CBAEC3 /* UIImage+ImageEffects.m in Sources */ = {isa = PBXBuildFile; fileRef = DD2E172919FA84B800CBAEC3 /* UIImage+ImageEffects.m */; };
1314
DD4BA9C119DED8EF008D73EB /* right_answer.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = DD4BA9C019DED8EF008D73EB /* right_answer.mp3 */; };
1415
DD7282B919D6087C00077F54 /* Storyboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DD7282B719D6087C00077F54 /* Storyboard.storyboard */; };
@@ -36,6 +37,8 @@
3637
/* Begin PBXFileReference section */
3738
572BB1451B8383B3002DEE38 /* SCLTimerDisplay.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SCLTimerDisplay.h; sourceTree = "<group>"; };
3839
572BB1461B8383B3002DEE38 /* SCLTimerDisplay.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SCLTimerDisplay.m; sourceTree = "<group>"; };
40+
CF6FB1221C5926FC009715F3 /* SCLSwitchView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SCLSwitchView.h; sourceTree = "<group>"; };
41+
CF6FB1231C5926FC009715F3 /* SCLSwitchView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SCLSwitchView.m; sourceTree = "<group>"; };
3942
DD0D295B19D902DA00881F53 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = SCLAlertViewExample/Info.plist; sourceTree = SOURCE_ROOT; };
4043
DD24453A1BAC383600892117 /* SCLTextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SCLTextView.h; sourceTree = "<group>"; };
4144
DD24453B1BAC383600892117 /* SCLTextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SCLTextView.m; sourceTree = "<group>"; };
@@ -172,6 +175,8 @@
172175
572BB1461B8383B3002DEE38 /* SCLTimerDisplay.m */,
173176
DD24453A1BAC383600892117 /* SCLTextView.h */,
174177
DD24453B1BAC383600892117 /* SCLTextView.m */,
178+
CF6FB1221C5926FC009715F3 /* SCLSwitchView.h */,
179+
CF6FB1231C5926FC009715F3 /* SCLSwitchView.m */,
175180
);
176181
path = SCLAlertView;
177182
sourceTree = "<group>";
@@ -279,6 +284,7 @@
279284
DD2E172A19FA84B800CBAEC3 /* UIImage+ImageEffects.m in Sources */,
280285
DDB15FCB19D5B88A00173158 /* ViewController.m in Sources */,
281286
DDB15FD419D5B8DB00173158 /* main.m in Sources */,
287+
CF6FB1241C5926FC009715F3 /* SCLSwitchView.m in Sources */,
282288
DDB15FE119D5D85B00173158 /* SCLAlertViewStyleKit.m in Sources */,
283289
DD24453C1BAC383600892117 /* SCLTextView.m in Sources */,
284290
572BB1471B8383B3002DEE38 /* SCLTimerDisplay.m in Sources */,

SCLAlertView/SCLAlertView.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#endif
1414
#import "SCLButton.h"
1515
#import "SCLTextView.h"
16+
#import "SCLSwitchView.h"
1617

1718
typedef NSAttributedString* (^SCLAttributedFormatBlock)(NSString *value);
1819
typedef void (^SCLDismissBlock)(void);
@@ -247,6 +248,12 @@ typedef NS_ENUM(NSInteger, SCLAlertViewBackground)
247248
*/
248249
- (void)addCustomTextField:(UITextField *)textField;
249250

251+
/** Add a switch view
252+
*
253+
* @param label The label displayed for the switch.
254+
*/
255+
- (SCLSwitchView *)addSwitchViewWithLabel:(NSString *)label;
256+
250257
/** Add Timer Display
251258
*
252259
* @param buttonIndex The index of the button to add the timer display to.

SCLAlertView/SCLAlertView.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,27 @@ - (void)addActivityIndicatorView
498498
[_circleView addSubview:_activityIndicatorView];
499499
}
500500

501+
#pragma mark - SwitchView
502+
503+
- (SCLSwitchView *)addSwitchViewWithLabel:(NSString *)label
504+
{
505+
// Add switch view
506+
SCLSwitchView *switchView = [[SCLSwitchView alloc] initWithFrame:CGRectMake(0, 0, self.windowWidth, 31.0f)];
507+
508+
// Update view height
509+
self.windowHeight += switchView.bounds.size.height + 10.0f;
510+
511+
if (label != nil)
512+
{
513+
switchView.labelText = label;
514+
}
515+
516+
[_contentView addSubview:switchView];
517+
[_inputs addObject:switchView];
518+
519+
return switchView;
520+
}
521+
501522
#pragma mark - TextField
502523

503524
- (SCLTextView *)addTextField:(NSString *)title

SCLAlertView/SCLSwitchView.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// SCLSwitchView.h
3+
// SCLAlertView
4+
//
5+
// Created by André Felipe Santos on 27/01/16.
6+
// Copyright © 2016 AnyKey Entertainment. All rights reserved.
7+
//
8+
9+
#if defined(__has_feature) && __has_feature(modules)
10+
@import UIKit;
11+
#else
12+
#import <UIKit/UIKit.h>
13+
#endif
14+
15+
@interface SCLSwitchView : UIView
16+
17+
@property (strong, nonatomic) UIColor *tintColor;
18+
@property (strong, nonatomic) UIColor *labelColor;
19+
@property (strong, nonatomic) UIFont *labelFont;
20+
@property (strong, nonatomic) NSString *labelText;
21+
@property (nonatomic, getter=isSelected) BOOL selected;
22+
23+
@end

SCLAlertView/SCLSwitchView.m

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
//
2+
// SCLSwitchView.m
3+
// SCLAlertView
4+
//
5+
// Created by André Felipe Santos on 27/01/16.
6+
// Copyright © 2016 AnyKey Entertainment. All rights reserved.
7+
//
8+
9+
#import "SCLSwitchView.h"
10+
#import "SCLMacros.h"
11+
12+
@interface SCLSwitchView ()
13+
14+
@property (strong, nonatomic) UISwitch *switchKnob;
15+
@property (strong, nonatomic) UILabel *switchLabel;
16+
17+
@end
18+
19+
#pragma mark
20+
21+
@implementation SCLSwitchView
22+
23+
#pragma mark - Constructors
24+
25+
- (instancetype)init
26+
{
27+
self = [super init];
28+
if (self)
29+
{
30+
[self setup];
31+
}
32+
return self;
33+
}
34+
35+
- (instancetype)initWithCoder:(NSCoder *)aDecoder
36+
{
37+
self = [super initWithCoder:aDecoder];
38+
if(self)
39+
{
40+
[self setup];
41+
}
42+
return self;
43+
}
44+
45+
- (instancetype)initWithFrame:(CGRect)frame
46+
{
47+
self = [super initWithFrame:frame];
48+
if (self)
49+
{
50+
[self setup];
51+
}
52+
return self;
53+
}
54+
55+
#pragma mark - Initialization
56+
57+
- (void)setup
58+
{
59+
// Add switch knob
60+
self.switchKnob = [[UISwitch alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.f)];
61+
[self addSubview:self.switchKnob];
62+
63+
// Add switch label
64+
CGFloat x, width, height;
65+
x = self.switchKnob.frame.size.width + 8.0f;
66+
width = self.frame.size.width - self.switchKnob.frame.size.width - 8.0f;
67+
height = self.switchKnob.frame.size.height;
68+
69+
self.switchLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 0.0f, width, height)];
70+
71+
NSString *switchFontFamily = @"HelveticaNeue-Bold";
72+
CGFloat switchFontSize = 12.0f;
73+
74+
self.switchLabel.numberOfLines = 1;
75+
self.switchLabel.textAlignment = NSTextAlignmentLeft;
76+
self.switchLabel.lineBreakMode = NSLineBreakByTruncatingTail;
77+
self.switchLabel.adjustsFontSizeToFitWidth = YES;
78+
self.switchLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
79+
self.switchLabel.minimumScaleFactor = 0.5f;
80+
self.switchLabel.font = [UIFont fontWithName:switchFontFamily size:switchFontSize];
81+
self.switchLabel.textColor = UIColorFromHEX(0x4D4D4D);
82+
83+
[self addSubview:self.switchLabel];
84+
}
85+
86+
#pragma mark - Getters
87+
88+
- (UIColor *)tintColor {
89+
return self.switchKnob.tintColor;
90+
}
91+
92+
- (UIColor *)labelColor {
93+
return self.switchLabel.textColor;
94+
}
95+
96+
- (UIFont *)labelFont {
97+
return self.switchLabel.font;
98+
}
99+
100+
- (NSString *)labelText {
101+
return self.switchLabel.text;
102+
}
103+
104+
- (BOOL)isSelected {
105+
return self.switchKnob.isOn;
106+
}
107+
108+
#pragma mark - Setters
109+
110+
- (void)setTintColor:(UIColor *)tintColor {
111+
self.switchKnob.onTintColor = tintColor;
112+
}
113+
114+
- (void)setLabelColor:(UIColor *)labelColor {
115+
self.switchLabel.textColor = labelColor;
116+
}
117+
118+
- (void)setLabelFont:(UIFont *)labelFont {
119+
self.switchLabel.font = labelFont;
120+
}
121+
122+
- (void)setLabelText:(NSString *)labelText {
123+
self.switchLabel.text = labelText;
124+
}
125+
126+
@end

0 commit comments

Comments
 (0)