dagronf / DSFStepperView

A custom stepper text field for macOS and iOS, supporting Swift, Objective-C, SwiftUI and Catalyst
MIT License
65 stars 8 forks source link
catalyst ios maccatalyst macos nsstepper nstextfield objective-c stepper swift swiftui

DSFStepperView

A custom stepper text field for macOS and iOS (Swift/SwiftUI/Objective-C/Catalyst).

drawing

drawing

Why?

I like the visual approach used with the SwiftUI settings pane, rather than having really small hit targets via the conventional up/down stepper control on macOS.

Features

Usage

Add DSFStepperView to your project via Swift Package Manager, or copy the sources in the Sources/DSFStepperView directly to your project

Demos are available in the Demo/ subfolder.

Via Interface Builder

Add a new NSView or UIView instance using Interface Builder, then change the class type to DSFStepperView

Programatically

let stepperView = DSFStepperView(frame: .zero)
stepperView.minimum = -1.0
stepperView.maximum = 1.0
stepperView.allowsEmpty = true
...
// Set the control's value to 0.5
stepperView.floatValue = 0.5

// Clear the control's value
stepperView.floatValue = nil

Receiving value changes

There are four methods for dynamically receiving value updates.

Receiving value changes via a delegate.

Implement the protocol DSFStepperViewDelegateProtocol on an object and set it as the delegate for an instance of DSFStepperView. Updates to the value will be received via the

func stepperView(_ view: DSFStepperView, didChangeValueTo value: NSNumber?)

interface.

Providing a change value block

You can provide a block to call via the onValueChange property on the stepper view.

self.stepper.onValueChange = { [weak self] newValue in
   Swift.print("Ordinal value did change to \(String(describing: newValue)) ")
}

Using the Combine framework on macOS 10.15, iOS 13 and later

You can use the Combine framework to subscribe to changes in the control. On 10.15 and later the control exposes the publisher publishedValue from which you can subscribe to value changes.

self.cancellable = myStepper.publishedValue.sink(receiveValue: { currentValue in
   if let c = currentValue {
      print("stepper is currently at \(c)")
   }
   else {
      print("stepper is currently empty")
   }
})

Binding to numberValue on an instance of the control.

You can use bindings to observe the numberValue member variable.

self.stepperObserver = self.observe(\.stepper.numberValue, options: [.new], changeHandler: { (_, value) in
   guard let val = value.newValue else { return }
   Swift.print("\(val)")
})

Value Display Formatting

If you want to allow non-integer values (such as 0.5), you will need to provide a NumberFormatter instance to format and validate the value in the field. DSFStepperView provides a default NumberFormatter which provides integer only values in the range (-∞ ... ∞) which you can override.

Using a number formatter also allows you to have a stepper that supports (for example) 1st, 2nd, 3rd, 4th when displaying.

let format = NumberFormatter()
format.numberStyle = .decimal

// Always display a single digit fractional value.
format.allowsFloats = true
format.minimumFractionDigits = 1
format.maximumFractionDigits = 1

stepperView.numberFormatter = format

In Interface Builder you can hook your instance's numberFormatter outlet to an instance of NumberFormatter in xib or storyboard.

Tooltips (macOS only)

You can specify a tooltip for the entire control the usual way using Interface Builder or programatically via

myStepperInstance.toolTip = "groovy!"

You can also provide individual tooltips for the components of the stepper via an optional function on the delegate.

@objc optional func stepperView(_ view: DSFStepperView, wantsTooltipTextforSegment segment: DSFStepperView.ToolTipSegment) -> String?

Using this method you can provide custom tooltips for the following stepper sections

Customizations

Properties

These properties can all be configured via Interface Builder or programatically.

SwiftUI

A SwiftUI wrapper is available, supporting both iOS and macOS. There is a demo project for iOS and macOS in the Demo subfolder.

Example ```swift struct ContentView: View { @State private var currentValue: CGFloat? = 23 @State private var isEnabled: Bool = true let configuration = DSFStepperView.SwiftUI.DisplaySettings( minimum: 0, maximum: 100, increment: 1 ) let style = DSFStepperView.SwiftUI.Style( font: DSFFont.systemFont(ofSize: 24, weight: .heavy), indicatorColor: DSFColor.systemBlue) var body: some View { DSFStepperView.SwiftUI( configuration: self.configuration, style: self.style, isEnabled: self.isEnabled, floatValue: self.$currentValue, onValueChange: { value in Swift.print("New value is \(String(describing: value))") } ) } } ```

History

License

MIT. Use it for anything you want, just attribute my work. Let me know if you do use it somewhere, I'd love to hear about it!

MIT License

Copyright (c) 2021 Darren Ford

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.