DSFStepperView

一个用于 macOS 和 iOS 的自定义步进文本字段(Swift/SwiftUI/Objective-C/Catalyst)。

drawing

drawing

为什么?

我喜欢 SwiftUI 设置面板中使用的视觉方法,而不是 macOS 上传统的上下步进控件那种 *非常* 小的点击目标。

特性

用法

通过 Swift Package Manager 将 DSFStepperView 添加到您的项目,或将 Sources/DSFStepperView 中的源文件直接复制到您的项目。

演示可在 Demo/ 子文件夹中找到。

通过 Interface Builder

使用 Interface Builder 添加一个新的 NSViewUIView 实例,然后将类类型更改为 DSFStepperView

以编程方式

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

接收值更改

有四种方法可以动态接收值更新。

通过委托接收值更改。

在一个对象上实现协议 DSFStepperViewDelegateProtocol,并将其设置为 DSFStepperView 实例的委托。值的更新将通过以下方式接收

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

接口。

提供一个更改值闭包

您可以通过步进视图上的 onValueChange 属性提供一个闭包来调用。

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

在 macOS 10.15、iOS 13 及更高版本上使用 Combine 框架

您可以使用 Combine 框架来订阅控件中的更改。在 10.15 及更高版本上,控件公开了发布者 publishedValue,您可以从中订阅值更改。

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

绑定到控件实例上的 numberValue

您可以使用绑定来观察 numberValue 成员变量。

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

值显示格式

如果您想允许非整数值(例如 0.5),您需要提供一个 NumberFormatter 实例来格式化和验证该字段中的值。 DSFStepperView 提供了一个默认的 NumberFormatter,它提供范围为 (-∞ ... ∞) 的仅整数值,您可以覆盖它。

使用数字格式化程序还可以让您拥有一个支持(例如)显示 1st、2nd、3rd、4th 的步进器。

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

在 Interface Builder 中,您可以将实例的 numberFormatter outlet 连接到 xib 或 storyboard 中的 NumberFormatter 实例。

工具提示(仅 macOS)

您可以使用 Interface Builder 以通常的方式或通过编程方式为整个控件指定工具提示。

myStepperInstance.toolTip = "groovy!"

您还可以通过委托上的可选函数为步进器的组件提供单独的工具提示。

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

使用此方法,您可以为以下步进器部分提供自定义工具提示。

自定义

属性

所有这些属性都可以通过 Interface Builder 或以编程方式进行配置。

SwiftUI

提供了一个 SwiftUI 包装器,支持 iOS 和 macOS。 Demo 子文件夹中有一个用于 iOS 和 macOS 的演示项目。

示例
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))")
         }
      )
   }
}

历史

许可证

MIT。 随意使用,只需署名我的作品即可。 如果您在某个地方使用了它,请告诉我,我很乐意听到它!

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.