Skip to content

Commit

Permalink
Issue #5 & #7
Browse files Browse the repository at this point in the history
This commit should allow to disable past selection and offer the
possibility to use clear button as today buttton
  • Loading branch information
hons committed Oct 10, 2014
1 parent 7091dd3 commit b42335c
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 46 deletions.
2 changes: 1 addition & 1 deletion THCalendarDatePicker.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "THCalendarDatePicker"
s.version = "0.1.1"
s.version = "0.2.0"
s.summary = "A DatePicker based on a custom calendar view"
s.homepage = "https://github.com/hons82/THCalendarDatePicker"
s.license = { :type => 'MIT', :file => 'LICENSE.md' }
Expand Down
22 changes: 22 additions & 0 deletions THCalendarDatePicker/THDatePickerViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,30 @@
@property (strong, nonatomic) UIColor *currentDateColor;

- (void)setDateHasItemsCallback:(BOOL (^)(NSDate * date))callback;

/*! Enable Clear Date Button
* \param allow should show "clear date" button
*/
- (void)setAllowClearDate:(BOOL)allow;

/*! Use Clear Date Button as "got to Today"
* \param beTodayButton should use "clear date" button as today
*/
- (void)setClearAsToday:(BOOL)beTodayButton;

/*! Should the view be closed on selection of a date
* \param autoClose should close view on selection
*/
- (void)setAutoCloseOnSelectDate:(BOOL)autoClose;

/*! Should it be possible to select dates in history
* \param disableHistorySelection should it be possible?
*/
- (void)setDisableHistorySelection:(BOOL)disableHistorySelection;

/*! Should it be possible to select dates in future
* \param disableFutureSelection should it be possible?
*/
- (void)setDisableFutureSelection:(BOOL)disableFutureSelection;

@end
54 changes: 39 additions & 15 deletions THCalendarDatePicker/THDatePickerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ @interface THDatePickerViewController () {
NSDate * _dateNoTime;
NSCalendar * _calendar;
BOOL _allowClearDate;
BOOL _clearAsToday;
BOOL _autoCloseOnSelectDate;
BOOL _disableHistorySelection;
BOOL _disableFutureSelection;
BOOL (^_dateHasItemsCallback)(NSDate *);
}
Expand Down Expand Up @@ -58,7 +60,9 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
// Custom initialization
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
_allowClearDate = NO;
_clearAsToday = NO;
_disableFutureSelection = NO;
_disableHistorySelection = NO;
}
return self;
}
Expand All @@ -67,19 +71,29 @@ +(THDatePickerViewController *)datePicker{
return [[THDatePickerViewController alloc] initWithNibName:@"THDatePickerViewController" bundle:nil];
}

- (void)setAllowClearDate:(BOOL)allow
{
- (void)setAllowClearDate:(BOOL)allow {
_allowClearDate = allow;
}

- (void)setClearAsToday:(BOOL)beTodayButton {
if (beTodayButton) {
[self setAllowClearDate:beTodayButton];
}
_clearAsToday = beTodayButton;
}

- (void)setAutoCloseOnSelectDate:(BOOL)autoClose
{
[self setAllowClearDate:!autoClose];
if (!_allowClearDate)
[self setAllowClearDate:!autoClose];
_autoCloseOnSelectDate = autoClose;
}

- (void)setDisableFutureSelection:(BOOL)disableFutureSelection
{
- (void)setDisableHistorySelection:(BOOL)disableHistorySelection {
_disableHistorySelection = disableHistorySelection;
}

- (void)setDisableFutureSelection:(BOOL)disableFutureSelection {
_disableFutureSelection = disableFutureSelection;
}

Expand Down Expand Up @@ -352,8 +366,7 @@ - (void)dateDayTapped:(THDateDay *)dateDay{
}
}

- (void)slideTransitionViewInDirection:(int)dir
{
- (void)slideTransitionViewInDirection:(int)dir {
dir = dir < 1 ? -1 : 1;
CGRect origFrame = self.calendarDaysView.frame;
CGRect outDestFrame = origFrame;
Expand All @@ -376,14 +389,12 @@ - (void)slideTransitionViewInDirection:(int)dir
}];
}

- (IBAction)nextMonthPressed:(id)sender
{
- (IBAction)nextMonthPressed:(id)sender {
[self incrementMonth:1];
[self slideTransitionViewInDirection:1];
}

- (IBAction)prevMonthPressed:(id)sender
{
- (IBAction)prevMonthPressed:(id)sender {
[self incrementMonth:-1];
[self slideTransitionViewInDirection:-1];
}
Expand All @@ -404,9 +415,14 @@ - (IBAction)okPressed:(id)sender {

- (IBAction)clearPressed:(id)sender {
if(self.clearBtn.enabled){
self.internalDate = nil;
[self.currentDay setSelected:NO];
self.currentDay = nil;
if (_clearAsToday) {
[self setDate:[NSDate date]];
[self redraw];
} else {
self.internalDate = nil;
[self.currentDay setSelected:NO];
self.currentDay = nil;
}
}
}

Expand All @@ -426,6 +442,13 @@ - (void) showClearButton {
self.clearBtn.frame = CGRectMake(curX, 5, buttonWidth, buttonHeight);
curX+=buttonWidth+5;
self.okBtn.frame = CGRectMake(curX, 5, buttonWidth, buttonHeight);
if (_clearAsToday) {
[self.clearBtn setImage:nil forState:UIControlStateNormal];
[self.clearBtn setTitle:NSLocalizedString(@"TODAY", @"Customize this for your language") forState:UIControlStateNormal];
} else {
[self.clearBtn setImage:[UIImage imageNamed:@"dialog_clear.png"] forState:UIControlStateNormal];
[self.clearBtn setTitle:nil forState:UIControlStateNormal];
}
}

- (void) hideClearButton {
Expand All @@ -448,7 +471,8 @@ - (BOOL)dateInFutureAndShouldBeDisabled:(NSDate *)dateToCompare
NSInteger comps = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);
currentDate = [calendar dateFromComponents:[calendar components:comps fromDate:currentDate]];
dateToCompare = [calendar dateFromComponents:[calendar components:comps fromDate:dateToCompare]];
return ([currentDate compare:dateToCompare] == NSOrderedAscending && _disableFutureSelection);
NSComparisonResult compResult = [currentDate compare:dateToCompare];
return (compResult == NSOrderedDescending && _disableHistorySelection) || (compResult == NSOrderedAscending && _disableFutureSelection);
}

- (BOOL)dateInCurrentMonth:(NSDate *)date{
Expand Down
35 changes: 15 additions & 20 deletions THCalendarDatePicker/THDatePickerViewController.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="5056" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6245" systemVersion="14A386a" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment defaultVersion="1792" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6238"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="THDatePickerViewController">
Expand All @@ -25,15 +24,12 @@
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Jpp-kp-HO9" userLabel="BottomView">
<rect key="frame" x="0.0" y="223" width="320" height="320"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="s0u-MY-tL4" userLabel="ControlView">
<rect key="frame" x="0.0" y="0.0" width="320" height="320"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="6">
<rect key="frame" x="10" y="3" width="63" height="36"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<state key="normal" image="dialog_cancel.png">
<color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
Expand All @@ -48,7 +44,6 @@
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="9">
<rect key="frame" x="10" y="48" width="63" height="33"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<state key="normal" image="arrow_left.png">
<color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
Expand All @@ -63,7 +58,6 @@
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="11">
<rect key="frame" x="250" y="48" width="63" height="33"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<state key="normal" image="arrow_right.png">
<color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
Expand All @@ -78,17 +72,14 @@
</button>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="68">
<rect key="frame" x="0.0" y="89" width="320" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="49">
<rect key="frame" x="2" y="113" width="316" height="207"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="8">
<rect key="frame" x="250" y="3" width="63" height="36"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<state key="normal" image="dialog_ok.png">
<color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
Expand All @@ -102,9 +93,14 @@
<action selector="okPressed:" destination="-1" eventType="touchUpInside" id="61"/>
</connections>
</button>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" fixedFrame="YES" text="Month" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="12">
<rect key="frame" x="81" y="48" width="161" height="32"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="7">
<rect key="frame" x="129" y="3" width="63" height="36"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMaxY="YES"/>
<rect key="frame" x="101" y="3" width="120" height="36"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<state key="normal" image="dialog_clear.png">
<color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
Expand All @@ -118,13 +114,6 @@
<action selector="clearPressed:" destination="-1" eventType="touchUpInside" id="59"/>
</connections>
</button>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" fixedFrame="YES" text="Month" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="12">
<rect key="frame" x="81" y="48" width="161" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstAttribute="width" constant="320" id="G5S-5U-Jba"/>
Expand All @@ -145,6 +134,7 @@
<constraint firstItem="Jpp-kp-HO9" firstAttribute="leading" secondItem="5" secondAttribute="leading" id="XhB-6E-W3s"/>
<constraint firstAttribute="bottom" secondItem="Jpp-kp-HO9" secondAttribute="bottom" id="hgJ-JC-x30"/>
</constraints>
<nil key="simulatedStatusBarMetrics"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
</view>
</objects>
Expand All @@ -157,4 +147,9 @@
<image name="dialog_clear.png" width="37" height="37"/>
<image name="dialog_ok.png" width="37" height="37"/>
</resources>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination" type="retina4"/>
</simulatedMetricsContainer>
</document>
3 changes: 2 additions & 1 deletion THCalendarDatePickerExample/Podfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '6.1'

pod 'KNSemiModalViewController'
pod 'KNSemiModalViewController'
2 changes: 1 addition & 1 deletion THCalendarDatePickerExample/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ DEPENDENCIES:
SPEC CHECKSUMS:
KNSemiModalViewController: 6fb1e2f6a4d84238b524cb705e91118b0fa6bbc9

COCOAPODS: 0.33.1
COCOAPODS: 0.34.2
Loading

0 comments on commit b42335c

Please sign in to comment.