Skip to content

Commit 99472d4

Browse files
committed
Added new constant for tracking capsule rounded styling
1 parent 9e570fd commit 99472d4

2 files changed

Lines changed: 122 additions & 163 deletions

File tree

TOSegmentedControl/TOSegmentedControl.h

Lines changed: 115 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -25,169 +25,146 @@
2525

2626
NS_ASSUME_NONNULL_BEGIN
2727

28-
/**
29-
A UI control that presents several
30-
options to the user in a horizontal, segmented layout.
31-
32-
Only one segment may be selected at a time and, if desired,
33-
may be designated as 'reversible' with an arrow icon indicating
34-
its direction.
35-
*/
36-
28+
/// A special value where the corner radius will automatically round into the capsule shape.
29+
FOUNDATION_EXPORT const CGFloat TOSegmentendControlCapsuleCornerRadius;
30+
31+
/// A UI control that presents several
32+
/// options to the user in a horizontal, segmented layout.
33+
///
34+
/// Only one segment may be selected at a time and, if desired,
35+
/// may be designated as 'reversible' with an arrow icon indicating
36+
/// its direction.
3737
NS_SWIFT_NAME(SegmentedControl)
3838
IB_DESIGNABLE @interface TOSegmentedControl : UIControl
3939

40-
/** The items currently assigned to this segmented control. (Can be a combination of strings and images) */
40+
/// The items currently assigned to this segmented control. (Can be a combination of strings and images)
4141
@property (nonatomic, copy, nullable) NSArray *items;
4242

43-
/** A block that is called whenever a segment is tapped. */
43+
/// A block that is called whenever a segment is tapped.
4444
@property (nonatomic, copy) void (^segmentTappedHandler)(NSInteger segmentIndex, BOOL reversed);
4545

46-
/** The number of segments this segmented control has. */
46+
/// The number of segments this segmented control has.
4747
@property (nonatomic, readonly) NSInteger numberOfSegments;
4848

49-
/** The index of the currently segment. (May be manually set) */
49+
/// The index of the currently segment. (May be manually set)
5050
@property (nonatomic, assign) NSInteger selectedSegmentIndex;
5151

52-
/** Whether the selected segment is also reveresed. */
52+
/// Whether the selected segment is also reveresed.
5353
@property (nonatomic, assign) BOOL selectedSegmentReversed;
5454

55-
/** The index values of all of the segments that are reversible. */
55+
/// The index values of all of the segments that are reversible.
5656
@property (nonatomic, strong) NSArray<NSNumber *> *reversibleSegmentIndexes;
5757

58-
/** The amount of rounding in the corners (Default is 9.0f) */
58+
/// The amount of rounding in the corners (Default is 8.0f up to iOS 18, and the capsule radius on iOS 26)
5959
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;
6060

61-
/** Set the background color of the track in the segmented control (Default is light grey) */
61+
/// Set the background color of the track in the segmented control (Default is light grey)
6262
@property (nonatomic, strong, null_resettable) IBInspectable UIColor *backgroundColor;
6363

64-
/** Set the color of the thumb view. (Default is white) */
64+
/// Set the color of the thumb view. (Default is white)
6565
@property (nonatomic, strong, null_resettable) IBInspectable UIColor *thumbColor;
6666

67-
/** Set the color of the separator lines between each item. (Default is dark grey) */
67+
/// Set the color of the separator lines between each item. (Default is dark grey)
6868
@property (nonatomic, strong, null_resettable) IBInspectable UIColor *separatorColor;
6969

70-
/** The color of the text labels / images (Default is black) */
70+
/// The color of the text labels / images (Default is black)
7171
@property (nonatomic, strong, null_resettable) IBInspectable UIColor *itemColor;
7272

73-
/** The color of the selected labels / images (Default is black) */
73+
/// The color of the selected labels / images (Default is black)
7474
@property (nonatomic, strong, null_resettable) IBInspectable UIColor *selectedItemColor;
7575

76-
/** The font of the text items (Default is system default at 10 points) */
76+
/// The font of the text items (Default is system default at 10 points)
7777
@property (nonatomic, strong, null_resettable) IBInspectable UIFont *textFont;
7878

79-
/** The font of the text item when it's been selected (Default is bold system default 10) */
79+
/// The font of the text item when it's been selected (Default is bold system default 10)
8080
@property (nonatomic, strong, null_resettable) IBInspectable UIFont *selectedTextFont;
8181

82-
/** The amount of insetting the thumb view is from the edge of the track (Default is 2.0f) */
82+
/// The amount of insetting the thumb view is from the edge of the track (Default is 2.0f)
8383
@property (nonatomic, assign) IBInspectable CGFloat thumbInset;
8484

85-
/** The opacity of the shadow surrounding the thumb view*/
85+
/// The opacity of the shadow surrounding the thumb view
8686
@property (nonatomic, assign) IBInspectable CGFloat thumbShadowOpacity;
8787

88-
/** The vertical offset of the shadow */
88+
/// The vertical offset of the shadow */
8989
@property (nonatomic, assign) IBInspectable CGFloat thumbShadowOffset;
9090

91-
/** The radius of the shadow */
91+
/// The radius of the shadow
9292
@property (nonatomic, assign) IBInspectable CGFloat thumbShadowRadius;
9393

94-
/**
95-
Creates a new segmented control with the provided items.
96-
97-
@param items An array of either images, or strings to display
98-
*/
94+
/// Creates a new segmented control with the provided items.
95+
///
96+
/// @param items An array of either images, or strings to display
9997
- (instancetype)initWithItems:(nullable NSArray *)items NS_SWIFT_NAME(init(items:));
10098

101-
/**
102-
Replaces the content of an existing segment with a new image.
10399

104-
@param image The image to set.
105-
@param index The index of the segment to set.
106-
*/
100+
/// Replaces the content of an existing segment with a new image.
101+
///
102+
/// @param image The image to set.
103+
/// @param index The index of the segment to set.
107104
- (void)setImage:(UIImage *)image forSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(set(_:forSegmentAt:));
108105

109-
/**
110-
Replaces the content of an existing segment with a new image,
111-
and optionally makes it reversible.
112-
113-
@param image The image to set.
114-
@param reversible Whether the item can be tapped multiple times to flip directions.
115-
@param index The index of the segment to set.
116-
*/
106+
/// Replaces the content of an existing segment with a new image,
107+
/// and optionally makes it reversible.
108+
///
109+
/// @param image The image to set.
110+
/// @param reversible Whether the item can be tapped multiple times to flip directions.
111+
/// @param index The index of the segment to set.
117112
- (void)setImage:(UIImage *)image reversible:(BOOL)reversible
118113
forSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(set(_:reversible:forSegmentAt:));
119114

120-
/**
121-
Returns the image that was assigned to a specific segment.
122-
Will return nil if the content at that segment is not an image.
123-
124-
@param index The index at which the image is located.
125-
*/
115+
/// Returns the image that was assigned to a specific segment.
116+
/// Will return nil if the content at that segment is not an image.
117+
///
118+
/// @param index The index at which the image is located.
126119
- (nullable UIImage *)imageForSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(image(forSegmentAt:));
127120

128-
/**
129-
Sets the content of a given segment to a text label.
130-
131-
@param title The text to display at the segment.
132-
@param index The index of the segment to set.
133-
*/
121+
/// Sets the content of a given segment to a text label.
122+
///
123+
/// @param title The text to display at the segment.
124+
/// @param index The index of the segment to set.
134125
- (void)setTitle:(NSString *)title forSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(set(_:forSegmentAt:));
135126

136-
/**
137-
Sets the content of a given segment to a text label, and
138-
optionally makes it reversible.
139-
140-
@param title The text to display at the segment.
141-
@param reversible Whether the item can be tapped multiple times to flip directions.
142-
@param index The index of the segment to set.
143-
*/
127+
/// Sets the content of a given segment to a text label, and
128+
/// optionally makes it reversible.
129+
///
130+
/// @param title The text to display at the segment.
131+
/// @param reversible Whether the item can be tapped multiple times to flip directions.
132+
/// @param index The index of the segment to set.
144133
- (void)setTitle:(NSString *)title reversible:(BOOL)reversible
145134
forSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(set(_:reversible:forSegmentAt:));
146135

147-
/**
148-
Returns the string of the title that was assigned to a specific segment.
149-
Will return nil if the content at that segment is not a string.
150-
151-
@param index The index at which the image is located.
152-
*/
136+
/// Returns the string of the title that was assigned to a specific segment.
137+
/// Will return nil if the content at that segment is not a string.
138+
///
139+
/// @param index The index at which the image is located.
153140
- (nullable NSString *)titleForSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(titleForSegment(for:));
154141

155-
/**
156-
Adds a new text segment to the end of the list.
157-
158-
@param title The title of the new item.
159-
*/
142+
/// Adds a new text segment to the end of the list.
143+
///
144+
/// @param title The title of the new item.
160145
- (void)addSegmentWithTitle:(NSString *)title NS_SWIFT_NAME(addSegment(withTitle:));
161146

162-
/**
163-
Adds a new text segment to the end of the list, and optionally makes it reversible.
164-
165-
@param title The title of the new item.
166-
@param reversible Whether the item is reversible or not.
167-
*/
147+
/// Adds a new text segment to the end of the list, and optionally makes it reversible.
148+
///
149+
/// @param title The title of the new item.
150+
/// @param reversible Whether the item is reversible or not.
168151
- (void)addSegmentWithTitle:(NSString *)title reversible:(BOOL)reversible NS_SWIFT_NAME(addSegment(withTitle:reversible:));
169152

170-
/**
171-
Adds a new image segment to the end of the list.
172-
173-
@param image The image of the new item.
174-
*/
153+
/// Adds a new image segment to the end of the list.
154+
///
155+
/// @param image The image of the new item.
175156
- (void)addSegmentWithImage:(UIImage *)image NS_SWIFT_NAME(addSegment(with:));
176157

177-
/**
178-
Adds a new image segment to the end of the list, and optionally makes it reversible.
179-
180-
@param image The image of the new item.
181-
@param reversible Whether the item is reversible or not.
182-
*/
158+
/// Adds a new image segment to the end of the list, and optionally makes it reversible.
159+
///
160+
/// @param image The image of the new item.
161+
/// @param reversible Whether the item is reversible or not.
183162
- (void)addSegmentWithImage:(UIImage *)image reversible:(BOOL)reversible NS_SWIFT_NAME(addSegment(with:reversible:));
184163

185-
/**
186-
Inserts a new image segment at the specified index.
187-
188-
@param image The image to set.
189-
@param index The index of the segment to which the image will be set.
190-
*/
164+
/// Inserts a new image segment at the specified index.
165+
///
166+
/// @param image The image to set.
167+
/// @param index The index of the segment to which the image will be set.
191168
- (void)insertSegmentWithImage:(UIImage *)image atIndex:(NSInteger)index NS_SWIFT_NAME(insertSegment(with:at:));
192169

193170
/**
@@ -200,92 +177,68 @@ IB_DESIGNABLE @interface TOSegmentedControl : UIControl
200177
- (void)insertSegmentWithImage:(UIImage *)image reversible:(BOOL)reversible
201178
atIndex:(NSInteger)index NS_SWIFT_NAME(insertSegment(with:reversible:at:));
202179

203-
/**
204-
Inserts a new title segment at the specified index.
205-
206-
@param title The title to set.
207-
@param index The index of the segment to which the image will be set.
208-
*/
180+
/// Inserts a new title segment at the specified index.
181+
///
182+
/// @param title The title to set.
183+
/// @param index The index of the segment to which the image will be set.
209184
- (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSInteger)index NS_SWIFT_NAME(insertSegment(withTitle:at:));
210185

211-
/**
212-
Inserts a new title segment at the specified index, and optionally makes it reversible.
213-
214-
@param title The title to set.
215-
@param reversible Whether the item is reversible or not.
216-
@param index The index of the segment to which the image will be set.
217-
*/
186+
/// Inserts a new title segment at the specified index, and optionally makes it reversible.
187+
///
188+
/// @param title The title to set.
189+
/// @param reversible Whether the item is reversible or not.
190+
/// @param index The index of the segment to which the image will be set.
218191
- (void)insertSegmentWithTitle:(NSString *)title reversible:(BOOL)reversible
219192
atIndex:(NSInteger)index NS_SWIFT_NAME(insertSegment(withTitle:reversible:at:));
220193

221-
/**
222-
Remove the last segment in the list
223-
*/
194+
/// Remove the last segment in the list
224195
- (void)removeLastSegment NS_SWIFT_NAME(removeLastSegment());
225196

226-
/**
227-
Removes the segment at the specified index.
228-
229-
@param index The index of the segment to remove.
230-
*/
197+
/// Removes the segment at the specified index.
198+
///
199+
/// @param index The index of the segment to remove.
231200
- (void)removeSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(removeSegment(at:));
232201

233-
/**
234-
Removes all of the items from this control.
235-
*/
202+
/// Removes all of the items from this control.
236203
- (void)removeAllSegments NS_SWIFT_NAME(removeAllSegments());
237204

238-
/**
239-
Enables or disables the segment at the specified index.
240-
241-
@param enabled Whether the segment is enabled or not.
242-
@param index The specific index to enable/disable.
243-
*/
205+
/// Enables or disables the segment at the specified index.
206+
///
207+
/// @param enabled Whether the segment is enabled or not.
208+
/// @param index The specific index to enable/disable.
244209
- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(setEnabled(_:forSegmentAt:));
245210

246-
/**
247-
Returns whether the segment at the specified index is currently enabled or not.
248-
249-
@param index The index to check.
250-
*/
211+
/// Returns whether the segment at the specified index is currently enabled or not.
212+
///
213+
/// @param index The index to check.
251214
- (BOOL)isEnabledForSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(isEnabledForSegment(at:));
252215

253-
/**
254-
Sets whether a specific segment is currently reversible or not.
255-
256-
@param reversible Whether the segment is reversible or not.
257-
@param index The specific index to enable/disable.
258-
*/
216+
/// Sets whether a specific segment is currently reversible or not.
217+
///
218+
/// @param reversible Whether the segment is reversible or not.
219+
/// @param index The specific index to enable/disable.
259220
- (void)setReversible:(BOOL)reversible forSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(setReversible(_:forSegmentAt:));
260221

261-
/**
262-
Returns whether the segment at the specified index is reversible or not.
263-
264-
@param index The index to check.
265-
*/
222+
/// Returns whether the segment at the specified index is reversible or not.
223+
///
224+
/// @param index The index to check.
266225
- (BOOL)isReversibleForSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(isReversibleForSegment(at:));
267226

268-
/**
269-
Sets whether a specific segment is currently in a reversed state or not.
270-
271-
@param reversed Whether the segment is currently reversed or not.
272-
@param index The specific index to enable/disable.
273-
*/
227+
/// Sets whether a specific segment is currently in a reversed state or not.
228+
///
229+
/// @param reversed Whether the segment is currently reversed or not.
230+
/// @param index The specific index to enable/disable.
274231
- (void)setReversed:(BOOL)reversed forSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(setReversed(_:forSegmentAt:));
275232

276-
/**
277-
Returns whether the segment at the specified index is currently reversed or not.
278-
279-
@param index The index to check.
280-
*/
233+
/// Returns whether the segment at the specified index is currently reversed or not.
234+
///
235+
/// @param index The index to check.
281236
- (BOOL)isReversedForSegmentAtIndex:(NSInteger)index NS_SWIFT_NAME(isReversed(at:));
282237

283-
/**
284-
Sets which segment is currently selected, and optionally play an animation during the transition.
285-
286-
@param selectedSegmentIndex The index of the segment to select.
287-
@param animated Whether the transition to the newly selected index is animated or not.
288-
*/
238+
/// Sets which segment is currently selected, and optionally play an animation during the transition.
239+
///
240+
/// @param selectedSegmentIndex The index of the segment to select.
241+
/// @param animated Whether the transition to the newly selected index is animated or not.
289242
- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex animated:(BOOL)animated NS_SWIFT_NAME(setSelectedSegmentIndex(_:animated:));
290243

291244
@end

TOSegmentedControl/TOSegmentedControl.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@
2424
#import "TOSegmentedControlSegment.h"
2525

2626
// ----------------------------------------------------------------
27-
// Static Members
27+
// Global members
2828

2929
// A cache to hold images generated for this view that may be shared.
3030
static NSMapTable *_imageTable = nil;
3131

32+
// A magic value used to indicate the rounding should be automatically based off height.
33+
const CGFloat TOSegmentendControlCapsuleCornerRadius = -1.0f;
34+
35+
// ----------------------------------------------------------------
36+
// Internal Members
37+
3238
// Statically referenced key names for the images stored in the map table.
3339
static NSString * const kTOSegmentedControlArrowImage = @"arrowIcon";
3440
static NSString * const kTOSegmentedControlSeparatorImage = @"separatorImage";

0 commit comments

Comments
 (0)