-
Notifications
You must be signed in to change notification settings - Fork 653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a method to customise the marker colours via the data source delegate and to detect long press in the calendar #192
Open
superprat
wants to merge
3
commits into
devinross:master
Choose a base branch
from
superprat:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,11 @@ @interface TKCalendarMonthTiles : UIView { | |
SEL action; | ||
|
||
int firstOfPrev,lastOfPrev; | ||
|
||
NSArray *marks; | ||
NSArray *marksColors; | ||
|
||
|
||
int today; | ||
BOOL markWasOnToday; | ||
|
||
|
@@ -58,7 +62,7 @@ @interface TKCalendarMonthTiles : UIView { | |
@property (strong,nonatomic) NSDate *monthDate; | ||
@property (nonatomic, strong) NSMutableArray *accessibleElements; | ||
|
||
- (id) initWithMonth:(NSDate*)date marks:(NSArray*)marks startDayOnSunday:(BOOL)sunday; | ||
- (id) initWithMonth:(NSDate*)date marks:(NSArray*)marks marksColors:(NSArray *)colors startDayOnSunday:(BOOL)sunday; | ||
- (void) setTarget:(id)target action:(SEL)action; | ||
|
||
- (void) selectDay:(int)day; | ||
|
@@ -214,16 +218,20 @@ + (NSArray*) rangeOfDatesInMonthGrid:(NSDate*)date startOnSunday:(BOOL)sunday{ | |
|
||
return [NSArray arrayWithObjects:firstDate,lastDate,nil]; | ||
} | ||
- (id) initWithMonth:(NSDate*)date marks:(NSArray*)markArray startDayOnSunday:(BOOL)sunday{ | ||
- (id) initWithMonth:(NSDate*)date marks:(NSArray*)markArray marksColors:(NSArray*)colors startDayOnSunday:(BOOL)sunday{ | ||
if(!(self=[super initWithFrame:CGRectZero])) return nil; | ||
|
||
firstOfPrev = -1; | ||
marks = markArray; | ||
marksColors = colors; | ||
|
||
_monthDate = date; | ||
startOnSunday = sunday; | ||
|
||
TKDateInformation dateInfo = [_monthDate dateInformationWithTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; | ||
firstWeekday = dateInfo.weekday; | ||
|
||
|
||
|
||
|
||
NSDate *prev = [_monthDate previousMonth]; | ||
|
@@ -277,7 +285,7 @@ - (CGRect) rectForCellAtIndex:(int)index{ | |
|
||
return CGRectMake(col*46, row*44+6, 47, 45); | ||
} | ||
- (void) drawTileInRect:(CGRect)r day:(int)day mark:(BOOL)mark font:(UIFont*)f1 font2:(UIFont*)f2{ | ||
- (void) drawTileInRect:(CGRect)r day:(int)day mark:(BOOL)mark markColor:(UIColor *)markColor font:(UIFont*)f1 font2:(UIFont*)f2{ | ||
|
||
NSString *str = [NSString stringWithFormat:@"%d",day]; | ||
|
||
|
@@ -289,13 +297,47 @@ - (void) drawTileInRect:(CGRect)r day:(int)day mark:(BOOL)mark font:(UIFont*)f1 | |
alignment: UITextAlignmentCenter]; | ||
|
||
if(mark){ | ||
r.size.height = 10; | ||
r.size.height = 13; | ||
r.origin.y += 18; | ||
|
||
[@"•" drawInRect: r | ||
withFont: f2 | ||
lineBreakMode: UILineBreakModeWordWrap | ||
alignment: UITextAlignmentCenter]; | ||
//draws the . in Tiles when not selected | ||
|
||
|
||
NSMutableParagraphStyle *paraAttr = [[NSMutableParagraphStyle defaultParagraphStyle ] mutableCopy]; | ||
[paraAttr setAlignment:UITextAlignmentCenter]; | ||
[paraAttr setLineBreakMode:UILineBreakModeWordWrap]; | ||
|
||
NSDictionary *attrsDictionary; | ||
|
||
if(markColor) | ||
{ | ||
attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:f2,NSFontAttributeName, | ||
markColor,NSForegroundColorAttributeName, | ||
|
||
paraAttr,NSParagraphStyleAttributeName, | ||
nil]; | ||
} | ||
else | ||
{ | ||
attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:f2,NSFontAttributeName, | ||
[UIColor grayColor],NSForegroundColorAttributeName, | ||
|
||
paraAttr,NSParagraphStyleAttributeName, | ||
nil]; | ||
} | ||
|
||
|
||
NSAttributedString *drawingString = [[NSAttributedString alloc] initWithString:@"•" attributes:attrsDictionary]; | ||
|
||
|
||
[drawingString drawInRect:r]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. drawInRect for NSAttributedString is available only for iOS 6 or newer. |
||
|
||
|
||
|
||
// [@"-" drawInRect: r | ||
// withFont: f2 | ||
// lineBreakMode: UILineBreakModeWordWrap | ||
// alignment: UITextAlignmentCenter]; | ||
} | ||
|
||
|
||
|
@@ -327,9 +369,9 @@ - (void) drawRect:(CGRect)rect { | |
for(int i = firstOfPrev;i<= lastOfPrev;i++){ | ||
r = [self rectForCellAtIndex:index]; | ||
if ([marks count] > 0) | ||
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] font:font font2:font2]; | ||
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] markColor:[marksColors objectAtIndex:index] font:font font2:font2]; | ||
else | ||
[self drawTileInRect:r day:i mark:NO font:font font2:font2]; | ||
[self drawTileInRect:r day:i mark:NO markColor:nil font:font font2:font2]; | ||
index++; | ||
} | ||
} | ||
|
@@ -343,9 +385,9 @@ - (void) drawRect:(CGRect)rect { | |
if(today == i) [[UIColor whiteColor] set]; | ||
|
||
if ([marks count] > 0) | ||
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] font:font font2:font2]; | ||
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] markColor:[marksColors objectAtIndex:index] font:font font2:font2]; | ||
else | ||
[self drawTileInRect:r day:i mark:NO font:font font2:font2]; | ||
[self drawTileInRect:r day:i mark:NO markColor:nil font:font font2:font2]; | ||
if(today == i) [color set]; | ||
index++; | ||
} | ||
|
@@ -355,9 +397,9 @@ - (void) drawRect:(CGRect)rect { | |
while(index % 7 != 0){ | ||
r = [self rectForCellAtIndex:index] ; | ||
if ([marks count] > 0) | ||
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] font:font font2:font2]; | ||
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] markColor:[marksColors objectAtIndex:index] font:font font2:font2]; | ||
else | ||
[self drawTileInRect:r day:i mark:NO font:font font2:font2]; | ||
[self drawTileInRect:r day:i mark:NO markColor:nil font:font font2:font2]; | ||
i++; | ||
index++; | ||
} | ||
|
@@ -441,7 +483,7 @@ - (NSDate*) dateSelected{ | |
|
||
} | ||
|
||
|
||
#define LONG_PRESS_THRESHOLD 3.0 | ||
- (void) reactToTouch:(UITouch*)touch down:(BOOL)down{ | ||
|
||
CGPoint p = [touch locationInView:self]; | ||
|
@@ -519,6 +561,13 @@ - (void) reactToTouch:(UITouch*)touch down:(BOOL)down{ | |
selectedDay = day; | ||
selectedPortion = portion; | ||
[target performSelector:action withObject:[NSArray arrayWithObject:[NSNumber numberWithInt:day]]]; | ||
|
||
|
||
|
||
// [NSObject cancelPreviousPerformRequestsWithTarget:target selector:@selector(tileDelay:)]; | ||
[NSObject cancelPreviousPerformRequestsWithTarget:target]; | ||
[target performSelector:@selector(tileDelay:) withObject:[NSArray arrayWithObject:[NSNumber numberWithInt:day]] afterDelay:LONG_PRESS_THRESHOLD]; | ||
|
||
|
||
}else if(down){ | ||
[target performSelector:action withObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:day],[NSNumber numberWithInt:portion],nil]]; | ||
|
@@ -527,6 +576,7 @@ - (void) reactToTouch:(UITouch*)touch down:(BOOL)down{ | |
} | ||
|
||
} | ||
|
||
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ | ||
//[super touchesBegan:touches withEvent:event]; | ||
[self reactToTouch:[touches anyObject] down:NO]; | ||
|
@@ -536,6 +586,9 @@ - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ | |
} | ||
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ | ||
[self reactToTouch:[touches anyObject] down:YES]; | ||
|
||
[NSObject cancelPreviousPerformRequestsWithTarget:target]; | ||
// [NSObject cancelPreviousPerformRequestsWithTarget:target selector:@selector(tileDelay:)]; | ||
} | ||
|
||
- (UILabel *) currentDay{ | ||
|
@@ -554,6 +607,9 @@ - (UILabel *) currentDay{ | |
return _currentDay; | ||
} | ||
- (UILabel *) dot{ | ||
|
||
//draws the . in Tiles when it is selected | ||
|
||
if(_dot==nil){ | ||
CGRect r = self.selectedImageView.bounds; | ||
r.origin.y += 29; | ||
|
@@ -607,7 +663,7 @@ - (id) initWithSundayAsFirst:(BOOL)s{ | |
self.backgroundColor = [UIColor grayColor]; | ||
|
||
sunday = s; | ||
currentTile = [[TKCalendarMonthTiles alloc] initWithMonth:[[NSDate date] firstOfMonth] marks:nil startDayOnSunday:sunday]; | ||
currentTile = [[TKCalendarMonthTiles alloc] initWithMonth:[[NSDate date] firstOfMonth] marks:nil marksColors:nil startDayOnSunday:sunday]; | ||
[currentTile setTarget:self action:@selector(tile:)]; | ||
|
||
CGRect r = CGRectMake(0, 0, self.tileBox.bounds.size.width, self.tileBox.bounds.size.height + self.tileBox.frame.origin.y); | ||
|
@@ -726,7 +782,12 @@ - (void) changeMonthAnimation:(UIView*)sender{ | |
|
||
NSArray *dates = [TKCalendarMonthTiles rangeOfDatesInMonthGrid:nextMonth startOnSunday:sunday]; | ||
NSArray *ar = [self.dataSource calendarMonthView:self marksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]]; | ||
TKCalendarMonthTiles *newTile = [[TKCalendarMonthTiles alloc] initWithMonth:nextMonth marks:ar startDayOnSunday:sunday]; | ||
NSArray *colors = [self.dataSource calendarMonthView:self colorsForMarksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]]; | ||
|
||
|
||
|
||
|
||
TKCalendarMonthTiles *newTile = [[TKCalendarMonthTiles alloc] initWithMonth:nextMonth marks:ar marksColors:colors startDayOnSunday:sunday]; | ||
[newTile setTarget:self action:@selector(tile:)]; | ||
|
||
|
||
|
@@ -847,8 +908,11 @@ - (void) selectDate:(NSDate*)date{ | |
|
||
NSArray *dates = [TKCalendarMonthTiles rangeOfDatesInMonthGrid:month startOnSunday:sunday]; | ||
NSArray *data = [self.dataSource calendarMonthView:self marksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]]; | ||
NSArray *colors = [self.dataSource calendarMonthView:self colorsForMarksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]]; | ||
|
||
TKCalendarMonthTiles *newTile = [[TKCalendarMonthTiles alloc] initWithMonth:month | ||
marks:data | ||
marks:data | ||
marksColors:colors | ||
startDayOnSunday:sunday]; | ||
[newTile setTarget:self action:@selector(tile:)]; | ||
[currentTile removeFromSuperview]; | ||
|
@@ -870,8 +934,10 @@ - (void) selectDate:(NSDate*)date{ | |
- (void) reload{ | ||
NSArray *dates = [TKCalendarMonthTiles rangeOfDatesInMonthGrid:[currentTile monthDate] startOnSunday:sunday]; | ||
NSArray *ar = [self.dataSource calendarMonthView:self marksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]]; | ||
NSArray *colors = [self.dataSource calendarMonthView:self colorsForMarksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]]; | ||
|
||
|
||
TKCalendarMonthTiles *refresh = [[TKCalendarMonthTiles alloc] initWithMonth:[currentTile monthDate] marks:ar startDayOnSunday:sunday]; | ||
TKCalendarMonthTiles *refresh = [[TKCalendarMonthTiles alloc] initWithMonth:[currentTile monthDate] marks:ar marksColors:colors startDayOnSunday:sunday]; | ||
[refresh setTarget:self action:@selector(tile:)]; | ||
|
||
[self.tileBox addSubview:refresh]; | ||
|
@@ -925,6 +991,51 @@ - (void) tile:(NSArray*)ar{ | |
|
||
} | ||
|
||
- (void) tileDelay:(NSArray*)ar{ | ||
|
||
if([ar count] < 2){ | ||
|
||
if([self.delegate respondsToSelector:@selector(calendarMonthView:didLongPressDate:)]) | ||
[self.delegate calendarMonthView:self didLongPressDate:[self dateSelected]]; | ||
|
||
}else{ | ||
|
||
int direction = [[ar lastObject] intValue]; | ||
UIButton *b = direction > 1 ? self.rightArrow : self.leftArrow; | ||
|
||
NSDate* newMonth = [self dateForMonthChange:b]; | ||
if ([self.delegate respondsToSelector:@selector(calendarMonthView:monthShouldChange:animated:)] && ![delegate calendarMonthView:self monthShouldChange:newMonth animated:YES]) | ||
return; | ||
|
||
if ([self.delegate respondsToSelector:@selector(calendarMonthView:monthWillChange:animated:)]) | ||
[self.delegate calendarMonthView:self monthWillChange:newMonth animated:YES]; | ||
|
||
|
||
|
||
[self changeMonthAnimation:b]; | ||
|
||
int day = [[ar objectAtIndex:0] intValue]; | ||
|
||
|
||
// thanks rafael | ||
TKDateInformation info = [[currentTile monthDate] dateInformationWithTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; | ||
info.day = day; | ||
|
||
NSDate *dateForMonth = [NSDate dateFromDateInformation:info timeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; | ||
[currentTile selectDay:day]; | ||
|
||
|
||
if([self.delegate respondsToSelector:@selector(calendarMonthView:didLongPressDate:)]) | ||
[self.delegate calendarMonthView:self didLongPressDate:dateForMonth]; | ||
|
||
if([self.delegate respondsToSelector:@selector(calendarMonthView:monthDidChange:animated:)]) | ||
[self.delegate calendarMonthView:self monthDidChange:dateForMonth animated:YES]; | ||
|
||
|
||
} | ||
|
||
} | ||
|
||
#pragma mark Properties | ||
- (UIImageView *) topBackground{ | ||
if(_topBackground==nil){ | ||
|
This file contains 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Attributes for NSAttributedString such as NSFontAttributeName are available only for iOS 6 or newer. If one needs to use NSAttributedString for iOS 5 or older, it must be done by using CoreText (kCTFontAttributeName and such).