Skip to content
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

add: slide label timestring support #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 7 additions & 28 deletions Pod/Classes/TTRangeSlider.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
IB_DESIGNABLE
@interface TTRangeSlider : UIControl <UIGestureRecognizerDelegate>

/**
* Optional delegate.
*/
@property (nonatomic, weak) IBOutlet id<TTRangeSliderDelegate> delegate;

/// slide label timestring support
- (id)initWithFrame:(CGRect)frame date:(NSDate*)date;
/// slide label timestring support
@property (nonatomic, strong) NSDate *currentDate;

/**
* The minimum possible value to select in the range
*/
Expand Down Expand Up @@ -148,34 +150,11 @@ IB_DESIGNABLE
/**
*Set slider line tint color between handles
*/
@property (nonatomic, strong) IBInspectable UIColor *tintColorBetweenHandles;
@property (nonatomic, strong) UIColor *tintColorBetweenHandles;

/**
*Set the slider line height (default 1.0)
*/
@property (nonatomic, assign) IBInspectable CGFloat lineHeight;

/**
*Slider line border color
*/
@property (nonatomic, strong) IBInspectable UIColor *lineBorderColor;

/**
*Slider line border width (default 0.0)
*/
@property (nonatomic, assign) IBInspectable CGFloat lineBorderWidth;

/**
*Define the two possibilities of label positions (above or below the handles)
*/
typedef NS_ENUM(NSInteger, LabelPosition) {
LabelPositionAbove,
LabelPositionBelow,
};

/**
*Set the label positions (default LabelPositionAbove)
*/
@property (nonatomic, assign) LabelPosition labelPosition;
@property (nonatomic, assign) CGFloat lineHeight;

@end
111 changes: 35 additions & 76 deletions Pod/Classes/TTRangeSlider.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,7 @@ @interface TTRangeSlider ()
// strong reference needed for UIAccessibilityContainer
// see http://stackoverflow.com/questions/13462046/custom-uiview-not-showing-accessibility-on-voice-over
@property (nonatomic, strong) NSMutableArray *accessibleElements;
@end

/**
An accessibility element that increments and decrements the left slider
*/
@interface TTRangeSliderLeftElement : UIAccessibilityElement
@end

/**
An accessibility element that increments and decrements the right slider
*/
@interface TTRangeSliderRightElement : UIAccessibilityElement
@end

static const CGFloat kLabelsFontSize = 12.0f;
Expand Down Expand Up @@ -72,14 +61,10 @@ - (void)initialiseControl {
_handleBorderColor = self.tintColor;

_labelPadding = 8.0;

_labelPosition = LabelPositionAbove;


//draw the slider line
self.sliderLine = [CALayer layer];
self.sliderLine.backgroundColor = self.tintColor.CGColor;
self.sliderLine.borderColor = self.lineBorderColor.CGColor;
self.sliderLine.borderWidth = self.lineBorderWidth;
[self.layer addSublayer:self.sliderLine];

//draw the track distline
Expand Down Expand Up @@ -195,14 +180,15 @@ - (id)initWithFrame:(CGRect)aRect
return self;
}

- (CGSize)intrinsicContentSize{
return CGSizeMake(UIViewNoIntrinsicMetric, 65);
- (id)initWithFrame:(CGRect)frame date:(NSDate*)date {
if (self = [self initWithFrame:frame]) {
self.currentDate = date;
}
return self;
}

-(void)prepareForInterfaceBuilder{
if (self.tintColorBetweenHandles == nil){
self.sliderLineBetweenHandles.backgroundColor = self.tintColor.CGColor;
}
- (CGSize)intrinsicContentSize{
return CGSizeMake(UIViewNoIntrinsicMetric, 65);
}


Expand Down Expand Up @@ -261,9 +247,30 @@ - (void)updateLabelValues {
self.maxLabel.string = @"";
return;
}

if (self.currentDate != nil) {
//date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd HH:mm:ss"];

NSDate *min_newDate = [[NSDate alloc] initWithTimeInterval:self.selectedMinimum sinceDate:self.currentDate];

NSDate *max_newDate = [[NSDate alloc] initWithTimeInterval:self.selectedMaximum sinceDate:self.currentDate];

NSString *min_newString = [dateFormatter stringFromDate:min_newDate];
NSString *max_newString = [dateFormatter stringFromDate:max_newDate];

self.minLabel.string = min_newString;
self.maxLabel.string = max_newString;

self.minLabelTextSize = [self.minLabel.string sizeWithAttributes:@{NSFontAttributeName:self.maxLabelFont}];
self.maxLabelTextSize = [self.maxLabel.string sizeWithAttributes:@{NSFontAttributeName:self.maxLabelFont}];
return ;
}

NSNumberFormatter *formatter = (self.numberFormatterOverride != nil) ? self.numberFormatterOverride : self.decimalNumberFormatter;


self.minLabel.string = [formatter stringFromNumber:@(self.selectedMinimum)];
self.maxLabel.string = [formatter stringFromNumber:@(self.selectedMaximum)];

Expand All @@ -290,15 +297,15 @@ - (void)updateHandlePositions {
}

- (void)updateLabelPositions {
//the centre points for the labels are X = the same x position as the relevant handle. Y = the y center of the handle plus or minus (depending on the label position) the handle size / 2 + padding + label size/2
//the centre points for the labels are X = the same x position as the relevant handle. Y = the y position of the handle minus half the height of the text label, minus some padding.
float padding = self.labelPadding;
float minSpacingBetweenLabels = 8.0f;

CGPoint leftHandleCentre = [self getCentreOfRect:self.leftHandle.frame];
CGPoint newMinLabelCenter = CGPointMake(leftHandleCentre.x, (self.leftHandle.frame.origin.y + (self.leftHandle.frame.size.height/2)) + ((self.labelPosition == LabelPositionAbove ? -1 : 1) * ((self.minLabel.frame.size.height/2) + padding + (self.leftHandle.frame.size.height/2))));
CGPoint newMinLabelCenter = CGPointMake(leftHandleCentre.x, self.leftHandle.frame.origin.y - (self.minLabel.frame.size.height/2) - padding);

CGPoint rightHandleCentre = [self getCentreOfRect:self.rightHandle.frame];
CGPoint newMaxLabelCenter = CGPointMake(rightHandleCentre.x, (self.rightHandle.frame.origin.y + (self.rightHandle.frame.size.height/2)) + ((self.labelPosition == LabelPositionAbove ? -1 : 1) * ((self.maxLabel.frame.size.height/2) + padding + (self.rightHandle.frame.size.height/2))));
CGPoint newMaxLabelCenter = CGPointMake(rightHandleCentre.x, self.rightHandle.frame.origin.y - (self.maxLabel.frame.size.height/2) - padding);

CGSize minLabelTextSize = self.minLabelTextSize;
CGSize maxLabelTextSize = self.maxLabelTextSize;
Expand Down Expand Up @@ -408,13 +415,9 @@ - (void)refresh {
[self updateAccessibilityElements];

//update the delegate
if ([self.delegate respondsToSelector:@selector(rangeSlider:didChangeSelectedMinimumValue:andMaximumValue:)] &&
(self.leftHandleSelected || self.rightHandleSelected)){

if (self.delegate && (self.leftHandleSelected || self.rightHandleSelected)){
[self.delegate rangeSlider:self didChangeSelectedMinimumValue:self.selectedMinimum andMaximumValue:self.selectedMaximum];
}

[self sendActionsForControlEvents:UIControlEventValueChanged];
}

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
Expand Down Expand Up @@ -534,7 +537,6 @@ - (void)setDisableRange:(BOOL)disableRange {
self.minLabel.hidden = YES;
} else {
self.leftHandle.hidden = NO;
self.minLabel.hidden = NO;
}
}

Expand Down Expand Up @@ -656,16 +658,6 @@ -(void)setLineHeight:(CGFloat)lineHeight{
[self setNeedsLayout];
}

-(void)setLineBorderColor:(UIColor *)lineBorderColor{
_lineBorderColor = lineBorderColor;
self.sliderLine.borderColor = [lineBorderColor CGColor];
}

-(void)setLineBorderWidth:(CGFloat)lineBorderWidth{
_lineBorderWidth = lineBorderWidth;
self.sliderLine.borderWidth = lineBorderWidth;
}

-(void)setLabelPadding:(CGFloat)labelPadding {
_labelPadding = labelPadding;
[self updateLabelPositions];
Expand Down Expand Up @@ -710,7 +702,7 @@ - (NSInteger)indexOfAccessibilityElement:(id)element

- (UIAccessibilityElement *)leftHandleAccessibilityElement
{
TTRangeSliderLeftElement *element = [[TTRangeSliderLeftElement alloc] initWithAccessibilityContainer:self];
UIAccessibilityElement *element = [[UIAccessibilityElement alloc] initWithAccessibilityContainer:self];
element.isAccessibilityElement = YES;
element.accessibilityLabel = self.minLabelAccessibilityLabel;
element.accessibilityHint = self.minLabelAccessibilityHint;
Expand All @@ -722,7 +714,7 @@ - (UIAccessibilityElement *)leftHandleAccessibilityElement

- (UIAccessibilityElement *)rightHandleAccessbilityElement
{
TTRangeSliderRightElement *element = [[TTRangeSliderRightElement alloc] initWithAccessibilityContainer:self];
UIAccessibilityElement *element = [[UIAccessibilityElement alloc] initWithAccessibilityContainer:self];
element.isAccessibilityElement = YES;
element.accessibilityLabel = self.maxLabelAccessibilityLabel;
element.accessibilityHint = self.maxLabelAccessibilityHint;
Expand All @@ -733,36 +725,3 @@ - (UIAccessibilityElement *)rightHandleAccessbilityElement
}

@end

@implementation TTRangeSliderLeftElement

- (void)accessibilityIncrement {
TTRangeSlider* slider = (TTRangeSlider*)self.accessibilityContainer;
slider.selectedMinimum += slider.step;
self.accessibilityValue = slider.minLabel.string;
}

- (void)accessibilityDecrement {
TTRangeSlider* slider = (TTRangeSlider*)self.accessibilityContainer;
slider.selectedMinimum -= slider.step;
self.accessibilityValue = slider.minLabel.string;
}

@end

@implementation TTRangeSliderRightElement

- (void)accessibilityIncrement {
TTRangeSlider* slider = (TTRangeSlider*)self.accessibilityContainer;
slider.selectedMaximum += slider.step;
self.accessibilityValue = slider.maxLabel.string;
}

- (void)accessibilityDecrement {
TTRangeSlider* slider = (TTRangeSlider*)self.accessibilityContainer;
slider.selectedMaximum -= slider.step;
self.accessibilityValue = slider.maxLabel.string;
}

@end

4 changes: 2 additions & 2 deletions Pod/Classes/TTRangeSliderDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

@protocol TTRangeSliderDelegate <NSObject>

@optional

/**
* Called when the RangeSlider values are changed
*/
-(void)rangeSlider:(TTRangeSlider *)sender didChangeSelectedMinimumValue:(float)selectedMinimum andMaximumValue:(float)selectedMaximum;

@optional

/**
* Called when the user has finished interacting with the RangeSlider
*/
Expand Down