Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A custom stepper text field for macOS and iOS, supporting Swift, Objective-C, SwiftUI and Catalyst

License

NotificationsYou must be signed in to change notification settings

dagronf/DSFStepperView

Repository files navigation

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 havingreally small hit targets via the conventional up/down stepper control on macOS.

Features

  • Support for macOS and iOS.
  • Cross-platform SwiftUI and Catalyst support for a consistent look.
  • IBDesignable support so you can see and configure your stepper views in Interface Builder.
  • Increment decrement buttons with repeat (click/press and hold to continuously increment/decrement)
  • Indicator bar to indicate the current fractional value of the control(optional)
  • Editable via keyboard(optional)
  • Empty field support (useful for items that are 'default' or 'inherited' values)(optional)
  • Set minimum/maximum and increment values. Support for float values (eg. increment by 0.01)
  • Font name/size/color
  • NumberFormatter support to display the value in the format you wish (for example, always showing 2 decimal places)(optional)
  • Specify a delegate to receive value change updates, or bind to the field's value.
  • Optional delegate to request tooltip strings depending on whether hovering over increment/decrement/text segments
  • Support for Combine publishing

Usage

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

Demos are available in theDemo/ subfolder.

Via Interface Builder

Add a newNSView orUIView instance using Interface Builder, then change the class type toDSFStepperView

Programatically

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

Receiving value changes

There are four methods for dynamically receiving value updates.

Receiving value changes via a delegate.

Implement the protocolDSFStepperViewDelegateProtocol on an object and set it as the delegate for an instance ofDSFStepperView. 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 theonValueChange property on the stepper view.

self.stepper.onValueChange={[weak self] newValueinSwift.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 publisherpublishedValue from which you can subscribe to value changes.

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

Binding tonumberValue on an instance of the control.

You can use bindings to observe thenumberValue member variable.

self.stepperObserver=self.observe(\.stepper.numberValue, options:[.new], changeHandler:{(_, value)inguardlet val= value.newValueelse{return}Swift.print("\(val)")})

Value Display Formatting

If you want to allow non-integer values (such as 0.5), you will need to provide aNumberFormatter 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.

letformat=NumberFormatter()format.numberStyle=.decimal// Always display a single digit fractional value.format.allowsFloats=trueformat.minimumFractionDigits=1format.maximumFractionDigits=1stepperView.numberFormatter= format

In Interface Builder you can hook your instance'snumberFormatter 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.

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

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

  • The decrement button
  • The increment button
  • The value text field

Customizations

Properties

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

  • allowsEmpty : Allow the field to be empty. Useful if you want to display (for example) an 'inherited' or 'default' label (Bool)
  • placeholder : The placeholder string to use when the field is empty (String)
  • minimum : The minimum value to be allowed in the view (CGFloat)
  • maximum : The maximum value to be allowed in the view (CGFloat)
  • increment : The amount to increment or decrement the count when using the buttons (CGFloat)
  • initialValue : The initial value to be displayed in the field (useful only for @IBDesignable support) (CGFloat)
  • fontName : The name of the font displaying the value (eg. Menlo). Defaults to the system font if the fontName cannot be resolved on the system. (String)
  • fontSize : The size (in pts) of the font displaying the value (CGFloat)
  • foregroundColor : The color of the font displaying the value (NSColor/UIColor)
  • indicatorColor : The color to draw the fractional value bar at the bottom of the control (NSColor/UIColor)
  • numberFormatter : An optional number formatter for formatting/validating values in the view
  • isEnabled : Enable or disable the control
  • font : The font for the text field

SwiftUI

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

Example
structContentView:View{@StateprivatevarcurrentValue:CGFloat?=23@StateprivatevarisEnabled:Bool=trueletconfiguration=DSFStepperView.SwiftUI.DisplaySettings(      minimum:0, maximum:100, increment:1)letstyle=DSFStepperView.SwiftUI.Style(      font:DSFFont.systemFont(ofSize:24, weight:.heavy),      indicatorColor:DSFColor.systemBlue)varbody:someView{DSFStepperView.SwiftUI(         configuration:self.configuration,         style:self.style,         isEnabled:self.isEnabled,         floatValue:self.$currentValue,         onValueChange:{ valueinSwift.print("New value is\(String(describing: value))")})}}

History

  • 4.0.0:
    • Move to minimum macOS 10.13 target, Swift 5.4.
    • Added inline SwiftUI previews for swiftui source.
  • 3.0.0: Explicitly define both static and dynamic libraries in the package.
  • 2.0.1: Fixedissue 4
  • 2.0.0:
    • Added iOS implementation for cross-platform compatibility (both SwiftUI and Catalyst support)
    • SeparatedfloatValue intofloatValue (Swift only) andnumberValue (an NSNumber for objc). If you have used bindings in your XIB that previously observedfloatValue you will need to update them to usenumberValue instead.
    • Removed the ability toset the value via the publisher in Combine (was a dumb idea).
  • 1.1.4: Added Objc demo, fixed delegate visibility
  • 1.1.3: Fixed issue with default SwiftUI initializer not exported.
  • 1.1.2: Some updates for accessibility
  • 1.1.1: Fixed issue with Combine not available before 10.15
  • 1.1.0: Added mouseover highlight for buttons, Combine publisher (10.15+), SwiftUI wrapper (10.15+).
  • 1.0.2: Fixed Issue where 10.14 and earlier didn't display the value (NumberFormatter changes)
  • 1.0.1: Fixed Bug #1 regarding disappearing button labels on Big Sur
  • 1.0.0: Initial release

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 LicenseCopyright (c) 2021 Darren FordPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 THESOFTWARE.

About

A custom stepper text field for macOS and iOS, supporting Swift, Objective-C, SwiftUI and Catalyst

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp