Skip to content

Commit 904cee9

Browse files
[CM-1208] Add Documentation (#12)
* [FEAT] started working on documentation. * [UPDATE] text format change [UPDATE] YStepper to StepperControl Alignment [UPDATE] edit periods * [UPDATE] review comment resolved
1 parent d929eee commit 904cee9

File tree

2 files changed

+230
-1
lines changed

2 files changed

+230
-1
lines changed

README.md

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,235 @@ Documentation is automatically generated from source code comments and rendered
1111

1212
Usage
1313
----------
14+
### `StepperControl` (UIKit)
15+
16+
`StepperControl` is a subclass of `UIControl` with an api similar to `UIStepper`.
17+
18+
### `Stepper` (SwiftUI)
19+
20+
`Stepper` is a struct that conforms to the SwiftUI `View` protocol.
21+
22+
### Initializers
23+
24+
Both `StepperControl` and `Stepper`can be initialized with the same five parameters (`StepperControl`uses `Stepper` internally):
25+
26+
```swift
27+
init(
28+
appearance: StepperControl.Appearance = .default,
29+
minimumValue: Double = 0,
30+
maximumValue: Double = 100,
31+
stepValue: Double = 1,
32+
value: Double = 0
33+
)
34+
```
35+
36+
The standard initializer lets you specify the appearance, minimum value, maximum value, step value and current value, although it provides sensible defaults for all of these.
37+
38+
`StepperControl` has an additional initializer:
39+
40+
```swift
41+
init?(coder: NSCoder)
42+
```
43+
44+
For use in Interface Builder or Storyboards (although we recommend that you build your UI in code).
45+
46+
A stepper created this way begins with the default appearance, but you can customize it at runtime by updating its `appearance` property.
47+
48+
### Customization
49+
50+
`StepperControl` and `Stepper` both have an `appearance` property of type `Appearance`.
51+
52+
`Appearance` lets you customize the stepper’s appearance. You have full control over the colors, typographies, and images used. The default appearance is dark mode compatible and WCAG 2.0 AA compliant for color contrast.
53+
54+
```swift
55+
/// Appearance for stepper that contains typography and color properties
56+
public struct Appearance {
57+
/// Typography of stepper value label
58+
public var textStyle: (textColor: UIColor, typography: Typography)
59+
/// Background color for stepper view
60+
public var backgroundColor: UIColor
61+
/// Border color for stepper view
62+
public var borderColor: UIColor
63+
/// Border width for stepper view
64+
public var borderWidth: CGFloat
65+
/// Delete button image
66+
public var deleteImage: UIImage
67+
/// Increment button image
68+
public var incrementImage: UIImage
69+
/// Decrement button image
70+
public var decrementImage: UIImage
71+
/// Stepper's layout properties such as spacing between views. Default is `.default`.
72+
public var layout: Layout
73+
/// Whether to show delete image or not
74+
var showDeleteImage: Bool
75+
}
76+
```
77+
78+
Appearance has `layout` property that can be used for customizing layout of the content.
79+
80+
```swift
81+
/// A collection of layout properties for the `StepperControl`
82+
public struct Layout: Equatable {
83+
/// The content inset from edges. Stepper "content" consists of the two buttons and the text label between them.
84+
/// Default is `{8, 16, 8, 16}`.
85+
public var contentInset: NSDirectionalEdgeInsets
86+
/// The horizontal spacing between the stepper buttons and label. Default is `8.0`.
87+
public var gap: CGFloat
88+
/// Stepper's shape. Default is `.capsule`.
89+
public var shape: Shape
90+
91+
/// Default stepper control layout.
92+
public static let `default` = Layout()
93+
}
94+
```
95+
96+
In `layout` there is a `shape` property that help decide the shape of `StepperControl` and `Stepper`
97+
98+
```swift
99+
/// Stepper's shape.
100+
public enum Shape: Equatable {
101+
/// None
102+
case none
103+
/// Rectangle
104+
case rectangle
105+
/// Rounded rectangle
106+
case roundRect(cornerRadius: CGFloat)
107+
/// Rounded rectangle that scales with Dynamic Type
108+
case scaledRoundRect(cornerRadius: CGFloat)
109+
/// Capsule
110+
case capsule
111+
}
112+
```
113+
114+
### Usage (UIKit)
115+
116+
1. **How to import?**
117+
118+
```swift
119+
import YStepper
120+
```
121+
122+
2. **Create a stepper**
123+
124+
```swift
125+
// Create stepper with default values
126+
let stepper = StepperControl()
127+
128+
// Add stepper to any view
129+
view.addSubview(stepper)
130+
```
131+
132+
3. **Customize and then update appearance**
133+
134+
```swift
135+
// Create a stepper with current value text color set to green
136+
var stepper = StepperControl(appearance: StepperControl.Appearance(textStyle: (textColor: .green, typography: .systemLabel)))
137+
138+
// Change the text color to red
139+
stepper.appearance.textStyle.textColor = .red
140+
```
141+
142+
4. **Update Stepper properties**
143+
144+
```swift
145+
// Set minimum value to 10 and maximum value to 101
146+
stepper.minimumValue = 10
147+
stepper.maximumValue = 101
148+
149+
// Set current value to 15
150+
stepper.value = 15
151+
```
152+
153+
5. **Receive change notifications**
154+
155+
To be notified when the value changes, simply use the target-action mechanism exactly as you would for `UIDatePicker` or `YCalendarPicker`.
156+
157+
```swift
158+
// Add target with action
159+
stepper.addTarget(self, action: #selector(onValueChange), for: .valueChanged)
160+
```
161+
162+
163+
### Usage (SwiftUI)
164+
165+
1. **How to import?**
166+
167+
```swift
168+
import YStepper
169+
```
170+
171+
2. **Create a stepper view**
172+
173+
`Stepper` conforms to SwiftUI's `View` protocol so we can directly integrate `Stepper` with any SwiftUI view.
174+
175+
```swift
176+
var body: some View {
177+
Stepper()
178+
}
179+
```
180+
181+
3. **Customize and then update appearance**
182+
183+
```swift
184+
struct CustomStepper {
185+
@State var stepper: Stepper = {
186+
// Create a stepper with text color set to green
187+
var stepper = Stepper()
188+
stepper.appearance.textStyle.textColor = .green
189+
return stepper
190+
}()
191+
}
192+
193+
extension CustomStepper: View {
194+
public var body: some View {
195+
VStack {
196+
stepper
197+
Button("Go Red") {
198+
// Change the text color to red
199+
stepper.appearance.textStyle.textColor = .red
200+
}
201+
}
202+
}
203+
}
204+
```
205+
206+
4. **Update Stepper properties**
207+
208+
```swift
209+
struct CustomStepper {
210+
@State var stepper = Stepper()
211+
}
212+
213+
extension CustomCalendar: View {
214+
var body: some View {
215+
VStack {
216+
stepper
217+
Button("Set Min/Max value") {
218+
// set minimum value to 10 and maximum value to 101
219+
stepper.minimumValue = 10
220+
stepper.maximumValue = 101
221+
}
222+
Button("Show current value as 15") {
223+
// update value
224+
stepper.value = 15
225+
}
226+
}
227+
}
228+
}
229+
```
230+
231+
5. **Receive change notifications** 
232+
233+
To be notified when the user update the value, you can use the `delegate` property and conform to the `StepperDelegate` protocol.
234+
235+
```swift
236+
extension DemoView: StepperDelegate {
237+
// Value was changed
238+
func valueDidChange(newValue: Double) {
239+
print("New value: \(value)")
240+
}
241+
}
242+
```
14243

15244
Installation
16245
----------

Sources/YStepper/UIKit/StepperControl+Appearance+Layout.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension StepperControl.Appearance {
1616
public var contentInset: NSDirectionalEdgeInsets
1717
/// The horizontal spacing between the stepper buttons and label. Default is `8.0`.
1818
public var gap: CGFloat
19-
/// Stepper's shape
19+
/// Stepper's shape. Default is `.capsule`.
2020
public var shape: Shape
2121

2222
/// Default stepper control layout.

0 commit comments

Comments
 (0)