From e8b38b3368fccf5ec113b034578e977dddcc0d96 Mon Sep 17 00:00:00 2001 From: "Marc C. Johnson" Date: Sun, 19 Sep 2021 03:46:14 -0500 Subject: [PATCH 1/3] Added new optional method to protocol to override selected min/max labels. If the method is implemented it acts as the boolean value to override the default values so I didn't think it necessary to add a designable to cut the feature off/on. It could be used for any type of transformation. My use case is that my floats are seconds 0 to end of the day and I want timestamps going up and down as the user slides the values. I chose something less meaningful for an example in my 2nd commit. --- Pod/Classes/TTRangeSlider.m | 15 ++++++++++++--- Pod/Classes/TTRangeSliderDelegate.h | 6 ++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Pod/Classes/TTRangeSlider.m b/Pod/Classes/TTRangeSlider.m index b1d71b3..f77284d 100644 --- a/Pod/Classes/TTRangeSlider.m +++ b/Pod/Classes/TTRangeSlider.m @@ -270,10 +270,19 @@ - (void)updateLabelValues { return; } - NSNumberFormatter *formatter = (self.numberFormatterOverride != nil) ? self.numberFormatterOverride : self.decimalNumberFormatter; + if (self.delegate && [self.delegate respondsToSelector:@selector(overrideLabelForRangeSlider:minMaxValue:)]) { - self.minLabel.string = [formatter stringFromNumber:@(self.selectedMinimum)]; - self.maxLabel.string = [formatter stringFromNumber:@(self.selectedMaximum)]; + self.minLabel.string = [self.delegate overrideLabelForRangeSlider:self minMaxValue:self.selectedMinimum]; + self.maxLabel.string = [self.delegate overrideLabelForRangeSlider:self minMaxValue:self.selectedMaximum]; + + } + else + { + NSNumberFormatter *formatter = (self.numberFormatterOverride != nil) ? self.numberFormatterOverride : self.decimalNumberFormatter; + + self.minLabel.string = [formatter stringFromNumber:@(self.selectedMinimum)]; + self.maxLabel.string = [formatter stringFromNumber:@(self.selectedMaximum)]; + } self.minLabelTextSize = [self.minLabel.string sizeWithAttributes:@{NSFontAttributeName:self.minLabelFont}]; self.maxLabelTextSize = [self.maxLabel.string sizeWithAttributes:@{NSFontAttributeName:self.maxLabelFont}]; diff --git a/Pod/Classes/TTRangeSliderDelegate.h b/Pod/Classes/TTRangeSliderDelegate.h index fa1b678..2d47ef4 100644 --- a/Pod/Classes/TTRangeSliderDelegate.h +++ b/Pod/Classes/TTRangeSliderDelegate.h @@ -28,4 +28,10 @@ */ - (void)didStartTouchesInRangeSlider:(TTRangeSlider *)sender; +/** + * Called when the user wants to override the selected min/max labels based upon their passed in float values. + */ + +- (nullable NSString *)overrideLabelForRangeSlider:(TTRangeSlider *_Nullable)sender minMaxValue:(float)selectedVal; + @end From 938cfe5f66c7c1ca1df2f82dbd246b352612d630 Mon Sep 17 00:00:00 2001 From: "Marc C. Johnson" Date: Sun, 19 Sep 2021 03:56:53 -0500 Subject: [PATCH 2/3] Added example of new implemented method for overriding labels translating them to hex in the bottom slider. I made a lot of adjustments to the storyboard because the last slider was below the fold in some of the smaller iPhone sizes. I also pushed them examples closer together and ended up replacing the last example (no labels) with overriden labels. I know it's important functionality but trivial to use as it's just a designable and was running out of room and a bit lazy to try and fit another slider in the limited space. There were a lot of warnings that changed other files in the project but they aren't relevant to the code actual code changes. --- .../Base.lproj/Main.storyboard | 75 +++++++++++-------- Example/RangeSliderDemo/ViewController.m | 25 +++++++ 2 files changed, 70 insertions(+), 30 deletions(-) diff --git a/Example/RangeSliderDemo/Base.lproj/Main.storyboard b/Example/RangeSliderDemo/Base.lproj/Main.storyboard index 6ffde32..7bc3a35 100644 --- a/Example/RangeSliderDemo/Base.lproj/Main.storyboard +++ b/Example/RangeSliderDemo/Base.lproj/Main.storyboard @@ -1,12 +1,9 @@ - - - - + + - - + @@ -23,7 +20,7 @@ - + @@ -36,7 +33,7 @@ - + @@ -68,13 +65,13 @@ - + @@ -105,13 +102,13 @@ - + @@ -142,8 +139,8 @@ - + + + + + + + + + + + + + + + + + diff --git a/Example/RangeSliderDemo/ViewController.m b/Example/RangeSliderDemo/ViewController.m index 77ea5af..1920e61 100644 --- a/Example/RangeSliderDemo/ViewController.m +++ b/Example/RangeSliderDemo/ViewController.m @@ -12,6 +12,8 @@ @interface ViewController () @property (weak, nonatomic) IBOutlet TTRangeSlider *rangeSlider; @property (weak, nonatomic) IBOutlet TTRangeSlider *rangeSliderCurrency; @property (weak, nonatomic) IBOutlet TTRangeSlider *rangeSliderCustom; +@property (weak, nonatomic) IBOutlet TTRangeSlider *rangeSliderOverridenLabels; + @end @implementation ViewController @@ -66,6 +68,8 @@ - (void)viewDidLoad { self.rangeSliderCustom.shadowRadius = 3; self.rangeSliderCustom.shadowOpacity = 0.5; + //override label range slider + self.rangeSliderOverridenLabels.delegate = self; } - (void)didReceiveMemoryWarning { @@ -83,4 +87,25 @@ -(void)rangeSlider:(TTRangeSlider *)sender didChangeSelectedMinimumValue:(float) } } +#pragma mark Optional override +- (NSString *)overrideLabelForRangeSlider:(TTRangeSlider *_Nullable)sender minMaxValue:(float)selectedVal{ + NSString *returnValue = nil; + if (sender == self.rangeSliderOverridenLabels) { + + NSString *str = [NSString stringWithFormat:@"%f", selectedVal]; + NSString *hexStr = [NSString stringWithFormat:@"%lX", + (unsigned long)[str integerValue]]; + if (sender.selectedMinimum == selectedVal) + { + returnValue = [NSString stringWithFormat:@"min:%@",hexStr]; + } + else + { + returnValue = [NSString stringWithFormat:@"max:%@",hexStr]; + } + } + return returnValue; +} + + @end From e9ab1aa55aff4c49a725b9913a3253996af74b2a Mon Sep 17 00:00:00 2001 From: MarcCJohnson Date: Sun, 19 Sep 2021 04:06:15 -0500 Subject: [PATCH 3/3] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index c913144..c6d109b 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,10 @@ To be notified when the slider’s value changes, register your action method wi Alternatively you can implement the `TTRangeSliderDelegate` protocol and respond to changes in the `rangeSlider:didChangeSelectedMinimumValue:andMaximumValue:` method. +## Overriding the selected minimum/maximum labels + +To override the selected min/max labels, implement the `TTRangeSliderDelegate` protocol and respond to changes in the `overrideLabelForRangeSlider:minMaxValue:` method. + Other customisation of the control is done using the following properties: #### `tintColor` The tintColor property (which you can also set in Interface Builder) sets the overall colour of the control, including the colour of the line, the two handles, and the labels.