HMSegmentedControl.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // HMSegmentedControl.h
  3. // HMSegmentedControl
  4. //
  5. // Created by Hesham Abd-Elmegid on 23/12/12.
  6. // Copyright (c) 2012 Hesham Abd-Elmegid. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. typedef void (^IndexChangeBlock)(NSInteger index);
  10. typedef enum {
  11. HMSegmentedControlSelectionStyleTextWidthStripe, // Indicator width will only be as big as the text width
  12. HMSegmentedControlSelectionStyleFullWidthStripe, // Indicator width will fill the whole segment
  13. HMSegmentedControlSelectionStyleBox, // A rectangle that covers the whole segment
  14. HMSegmentedControlSelectionStyleArrow // An arrow in the middle of the segment pointing up or down depending on `HMSegmentedControlSelectionIndicatorLocation`
  15. } HMSegmentedControlSelectionStyle;
  16. typedef enum {
  17. HMSegmentedControlSelectionIndicatorLocationUp,
  18. HMSegmentedControlSelectionIndicatorLocationDown,
  19. HMSegmentedControlSelectionIndicatorLocationNone // No selection indicator
  20. } HMSegmentedControlSelectionIndicatorLocation;
  21. typedef enum {
  22. HMSegmentedControlSegmentWidthStyleFixed, // Segment width is fixed
  23. HMSegmentedControlSegmentWidthStyleDynamic, // Segment width will only be as big as the text width (including inset)
  24. } HMSegmentedControlSegmentWidthStyle;
  25. enum {
  26. HMSegmentedControlNoSegment = -1 // Segment index for no selected segment
  27. };
  28. typedef enum {
  29. HMSegmentedControlTypeText,
  30. HMSegmentedControlTypeImages,
  31. HMSegmentedControlTypeTextImages
  32. } HMSegmentedControlType;
  33. @interface HMSegmentedControl : UIControl
  34. @property (nonatomic, strong) NSArray *sectionTitles;
  35. @property (nonatomic, strong) NSArray *sectionImages;
  36. @property (nonatomic, strong) NSArray *sectionSelectedImages;
  37. /*
  38. Provide a block to be executed when selected index is changed.
  39. Alternativly, you could use `addTarget:action:forControlEvents:`
  40. */
  41. @property (nonatomic, copy) IndexChangeBlock indexChangeBlock;
  42. /*
  43. Font for segments names when segmented control type is `HMSegmentedControlTypeText`
  44. Default is [UIFont fontWithName:@"STHeitiSC-Light" size:18.0f]
  45. */
  46. @property (nonatomic, strong) UIFont *font;
  47. /*
  48. Text color for segments names when segmented control type is `HMSegmentedControlTypeText`
  49. Default is [UIColor blackColor]
  50. */
  51. @property (nonatomic, strong) UIColor *textColor;
  52. /*
  53. Text color for selected segment name when segmented control type is `HMSegmentedControlTypeText`
  54. Default is [UIColor blackColor]
  55. */
  56. @property (nonatomic, strong) UIColor *selectedTextColor;
  57. /*
  58. Segmented control background color.
  59. Default is [UIColor whiteColor]
  60. */
  61. @property (nonatomic, strong) UIColor *backgroundColor;
  62. /*
  63. Color for the selection indicator stripe/box
  64. Default is R:52, G:181, B:229
  65. */
  66. @property (nonatomic, strong) UIColor *selectionIndicatorColor;
  67. /*
  68. Opacity for the seletion inficator box.
  69. Default is 0.2
  70. */
  71. @property (nonatomic) CGFloat selectionIndicatorBoxOpacity;
  72. /*
  73. Specifies the style of the control
  74. Default is `HMSegmentedControlTypeText`
  75. */
  76. @property (nonatomic, assign) HMSegmentedControlType type;
  77. /*
  78. Specifies the style of the selection indicator.
  79. Default is `HMSegmentedControlSelectionStyleTextWidthStripe`
  80. */
  81. @property (nonatomic, assign) HMSegmentedControlSelectionStyle selectionStyle;
  82. /*
  83. Specifies the style of the segment's width.
  84. Default is `HMSegmentedControlSegmentWidthStyleFixed`
  85. */
  86. @property (nonatomic, assign) HMSegmentedControlSegmentWidthStyle segmentWidthStyle;
  87. /*
  88. Specifies the location of the selection indicator.
  89. Default is `HMSegmentedControlSelectionIndicatorLocationUp`
  90. */
  91. @property (nonatomic, assign) HMSegmentedControlSelectionIndicatorLocation selectionIndicatorLocation;
  92. /*
  93. Default is NO. Set to YES to allow for adding more tabs than the screen width could fit.
  94. When set to YES, segment width will be automatically set to the width of the biggest segment's text or image,
  95. otherwise it will be equal to the width of the control's frame divided by the number of segments.
  96. As of v 1.4 this is no longer needed. The control will manage scrolling automatically based on tabs sizes.
  97. */
  98. @property(nonatomic, getter = isScrollEnabled) BOOL scrollEnabled DEPRECATED_ATTRIBUTE;
  99. /*
  100. Default is YES. Set to NO to deny scrolling by dragging the scrollView by the user.
  101. */
  102. @property(nonatomic, getter = isUserDraggable) BOOL userDraggable;
  103. /*
  104. Default is YES. Set to NO to deny any touch events by the user.
  105. */
  106. @property(nonatomic, getter = isTouchEnabled) BOOL touchEnabled;
  107. /*
  108. Index of the currently selected segment.
  109. */
  110. @property (nonatomic, assign) NSInteger selectedSegmentIndex;
  111. /*
  112. Height of the selection indicator. Only effective when `HMSegmentedControlSelectionStyle` is either `HMSegmentedControlSelectionStyleTextWidthStripe` or `HMSegmentedControlSelectionStyleFullWidthStripe`.
  113. Default is 5.0
  114. */
  115. @property (nonatomic, readwrite) CGFloat selectionIndicatorHeight;
  116. /*
  117. Edge insets for the selection indicator.
  118. NOTE: This does not affect the bounding box of HMSegmentedControlSelectionStyleBox
  119. When HMSegmentedControlSelectionIndicatorLocationUp is selected, bottom edge insets are not used
  120. When HMSegmentedControlSelectionIndicatorLocationDown is selected, top edge insets are not used
  121. Defaults are top: 0.0f
  122. left: 0.0f
  123. bottom: 0.0f
  124. right: 0.0f
  125. */
  126. @property (nonatomic, readwrite) UIEdgeInsets selectionIndicatorEdgeInsets;
  127. /*
  128. Inset left and right edges of segments. Only effective when `scrollEnabled` is set to YES.
  129. Default is UIEdgeInsetsMake(0, 5, 0, 5)
  130. */
  131. @property (nonatomic, readwrite) UIEdgeInsets segmentEdgeInset;
  132. /*
  133. Default is YES. Set to NO to disable animation during user selection.
  134. */
  135. @property (nonatomic) BOOL shouldAnimateUserSelection;
  136. - (id)initWithSectionTitles:(NSArray *)sectiontitles;
  137. - (id)initWithSectionImages:(NSArray *)sectionImages sectionSelectedImages:(NSArray *)sectionSelectedImages;
  138. - (instancetype)initWithSectionImages:(NSArray *)sectionImages sectionSelectedImages:(NSArray *)sectionSelectedImages titlesForSections:(NSArray *)sectiontitles;
  139. - (void)setSelectedSegmentIndex:(NSUInteger)index animated:(BOOL)animated;
  140. - (void)setIndexChangeBlock:(IndexChangeBlock)indexChangeBlock;
  141. @end