Skip to content

Commit

Permalink
add labelsFixed
Browse files Browse the repository at this point in the history
  • Loading branch information
WorldDownTown committed Mar 14, 2017
1 parent 2b1e915 commit a71009f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 35 deletions.
44 changes: 25 additions & 19 deletions Demo/RangeSeekSliderDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<viewControllerLayoutGuide type="bottom" id="FIB-kF-Y0q"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="dDy-w5-TsV">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="817"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" translatesAutoresizingMaskIntoConstraints="NO" id="wcl-zp-wAu">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="817"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Standard Range:" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="C9a-jS-yyk">
<rect key="frame" x="16" y="20" width="128" height="21"/>
Expand Down Expand Up @@ -154,18 +154,6 @@
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="hideLabels" value="YES"/>
<userDefinedRuntimeAttribute type="number" keyPath="minValue">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="maxValue">
<real key="value" value="100"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="selectedMinValue">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="selectedMaxValue">
<real key="value" value="100"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Custom Label String:" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="qwj-Dy-ogm">
Expand All @@ -176,7 +164,7 @@
</label>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="gdz-ut-dL8" customClass="RangeSeekSlider" customModule="RangeSeekSlider">
<rect key="frame" x="16" y="631" width="343" height="65"/>
<color key="tintColor" red="0.40000000000000002" green="0.40000000000000002" blue="0.40000000000000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="tintColor" red="0.80392158030000005" green="0.80392158030000005" blue="0.80392158030000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="65" id="wa9-1w-B3f"/>
</constraints>
Expand All @@ -187,18 +175,34 @@
<userDefinedRuntimeAttribute type="number" keyPath="minDistance">
<real key="value" value="1"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="selectedMaxValue">
<real key="value" value="67"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="labelsFixed" value="YES"/>
<userDefinedRuntimeAttribute type="color" keyPath="initialColor">
<color key="value" red="0.80392158030000005" green="0.80392158030000005" blue="0.80392158030000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="selectedMinValue">
<real key="value" value="0.0"/>
<userDefinedRuntimeAttribute type="color" keyPath="colorBetweenHandles">
<color key="value" red="0.94509803920000002" green="0.32156862749999998" blue="0.63921568630000003" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="handleColor">
<color key="value" red="0.94509803920000002" green="0.32156862749999998" blue="0.63921568630000003" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="minLabelColor">
<color key="value" red="0.94509803920000002" green="0.32156862749999998" blue="0.63921568630000003" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="maxLabelColor">
<color key="value" red="0.94509803920000002" green="0.32156862749999998" blue="0.63921568630000003" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="minValue">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="maxValue">
<real key="value" value="67"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="selectedMinValue">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="selectedMaxValue">
<real key="value" value="67"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="By WorldDownTown" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="l8Q-0R-HSN">
Expand Down Expand Up @@ -255,6 +259,8 @@
</constraints>
</view>
<navigationItem key="navigationItem" title="RangeSeekSlider" id="BuP-se-RZI"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="375" height="817"/>
<connections>
<outlet property="rangeSlider" destination="evq-sI-chw" id="k72-tE-yA5"/>
<outlet property="rangeSliderCurrency" destination="aHO-Yd-TsS" id="hMn-Ta-0mN"/>
Expand Down
1 change: 1 addition & 0 deletions Demo/RangeSeekSliderDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ final class ViewController: UIViewController {
rangeSliderCustomString.tintColor = #colorLiteral(red: 0.8666666667, green: 0.8666666667, blue: 0.8666666667, alpha: 1)
rangeSliderCustomString.numberFormatter.locale = Locale.current
rangeSliderCustomString.numberFormatter.numberStyle = .currency
rangeSliderCustomString.labelsFixed = true
rangeSliderCustomString.initialColor = #colorLiteral(red: 0.8666666667, green: 0.8666666667, blue: 0.8666666667, alpha: 1)
}

Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ The font of the maximum value text label. If not set, the default is system font
Each handle in the slider has a label above it showing the current selected value. If you change number format, update each properties of `NumberFormatter`. By default, this is displayed as a decimal format.

#### `hideLabels`
When set to `true` the labels above the slider controls will be hidden. Default is false.
When set to `true` the labels above the slider controls will be hidden. Default is `false`.

#### `labelsFixed`
Fixes the labels above the slider controls. If `true`, labels will be fixed to both ends. Otherwise labels will move with the handles. Default is `false`.

#### `minDistance`
The minimum distance the two selected slider values must be apart. Default is `0.0`.
Expand All @@ -95,15 +98,15 @@ If set it will update the color of the handle borders. Default is `tintColor`.
The color of the entire slider when the handle is set to the minimum value and the maximum value. Default is nil.

#### `disableRange`
If true, the control will mimic a normal slider and have only one handle rather than a range.
If `true`, the control will mimic a normal slider and have only one handle rather than a range.

In this case, the selectedMinValue will be not functional anymore. Use selectedMaxValue instead to determine the value the user has selected.

#### `enableStep`
If true the control will snap to point at each `step` (property) between minValue and maxValue. Default value is disabled.
If `true` the control will snap to point at each `step` (property) between minValue and maxValue. Default value is disabled.

#### `step`
If `enableStep` is true, this controls the value of each step. E.g. if this value is 20, the control will snap to values 20,40,60...etc. Set this is you enable the `enableStep` property.
If `enableStep` is `true`, this controls the value of each step. E.g. if this value is 20, the control will snap to values 20,40,60...etc. Set this is you enable the `enableStep` property.

#### `handleImage`
If set the image passed will be used for the handles.
Expand Down
2 changes: 1 addition & 1 deletion RangeSeekSlider.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'RangeSeekSlider'
s.version = '1.3.0'
s.version = '1.4.0'
s.summary = 'RangeSeedSlider provides a customizable range slider like a UISlider.'
s.description = <<-DESC
RangeSeedSlider provides a customizable range slider like a UISlider.
Expand Down
55 changes: 44 additions & 11 deletions Sources/RangeSeekSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ import UIKit
public weak var delegate: RangeSeekSliderDelegate?

/// The minimum possible value to select in the range
@IBInspectable public var minValue: CGFloat = 0.0
@IBInspectable public var minValue: CGFloat = 0.0 {
didSet {
refresh()
}
}

/// The maximum possible value to select in the range
@IBInspectable public var maxValue: CGFloat = 100.0
@IBInspectable public var maxValue: CGFloat = 100.0 {
didSet {
refresh()
}
}

/// The preselected minumum value
/// (note: This should be less than the selectedMaxValue)
Expand Down Expand Up @@ -85,7 +93,15 @@ import UIKit
}()

/// Hides the labels above the slider controls. true = labels will be hidden. false = labels will be shown. Default is false.
@IBInspectable public var hideLabels: Bool = false
@IBInspectable public var hideLabels: Bool = false {
didSet {
minLabel.isHidden = hideLabels
maxLabel.isHidden = hideLabels
}
}

/// fixes the labels above the slider controls. true: labels will be fixed to both ends. false: labels will move with the handles. Default is false.
@IBInspectable public var labelsFixed: Bool = false

/// The minimum distance the two selected slider values must be apart. Default is 0.
@IBInspectable public var minDistance: CGFloat = 0.0 {
Expand Down Expand Up @@ -437,11 +453,8 @@ import UIKit
}

private func updateLabelValues() {
if hideLabels {
minLabel.string = nil
maxLabel.string = nil
return
}
minLabel.isHidden = hideLabels
maxLabel.isHidden = hideLabels

if let replacedString = delegate?.rangeSeekSlider(self, stringForMinValue: selectedMinValue) {
minLabel.string = replacedString
Expand Down Expand Up @@ -517,6 +530,15 @@ import UIKit

private func updateLabelPositions() {
// the center 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.

minLabel.frame.size = minLabelTextSize
maxLabel.frame.size = maxLabelTextSize

if labelsFixed {
updateFixedLabelPositions()
return
}

let minSpacingBetweenLabels: CGFloat = 8.0

let newMinLabelCenter: CGPoint = CGPoint(x: leftHandle.frame.midX,
Expand All @@ -525,9 +547,6 @@ import UIKit
let newMaxLabelCenter: CGPoint = CGPoint(x: rightHandle.frame.midX,
y: rightHandle.frame.minY - (maxLabelTextSize.height / 2.0) - labelPadding)

minLabel.frame.size = minLabelTextSize
maxLabel.frame.size = maxLabelTextSize

let newLeftMostXInMaxLabel: CGFloat = newMaxLabelCenter.x - maxLabelTextSize.width / 2.0
let newRightMostXInMinLabel: CGFloat = newMinLabelCenter.x + minLabelTextSize.width / 2.0
let newSpacingBetweenTextLabels: CGFloat = newLeftMostXInMaxLabel - newRightMostXInMinLabel
Expand Down Expand Up @@ -566,6 +585,20 @@ import UIKit
}
}

private func updateFixedLabelPositions() {
minLabel.position = CGPoint(x: xPositionAlongLine(for: minValue),
y: sliderLine.frame.minY - (minLabelTextSize.height / 2.0) - (handleDiameter / 2.0) - labelPadding)
maxLabel.position = CGPoint(x: xPositionAlongLine(for: maxValue),
y: sliderLine.frame.minY - (maxLabelTextSize.height / 2.0) - (handleDiameter / 2.0) - labelPadding)
if minLabel.frame.minX < 0.0 {
minLabel.frame.origin.x = 0.0
}

if maxLabel.frame.maxX > frame.width {
maxLabel.frame.origin.x = frame.width - maxLabel.frame.width
}
}

fileprivate func refresh() {
if enableStep && step > 0.0 {
selectedMinValue = CGFloat(roundf(Float(selectedMinValue / step))) * step
Expand Down

0 comments on commit a71009f

Please sign in to comment.