HMSegmentedControl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // HMSegmentedControl.h
  3. // HMSegmentedControl
  4. //
  5. // Created by Hesham Abd-Elmegid on 23/12/12.
  6. // Copyright (c) 2012-2015 Hesham Abd-Elmegid. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. @class HMSegmentedControl;
  10. typedef void (^IndexChangeBlock)(NSInteger index);
  11. typedef NSAttributedString *(^HMTitleFormatterBlock)(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected);
  12. /**
  13. Segmented类型
  14. - HMSegmentedControlTypeText: 纯文字
  15. - HMSegmentedControlTypeImages: 纯图片
  16. - HMSegmentedControlTypeTextImages: 文字、图片组合
  17. */
  18. typedef NS_ENUM(NSInteger, HMSegmentedControlType)
  19. {
  20. HMSegmentedControlTypeText,
  21. HMSegmentedControlTypeImages,
  22. HMSegmentedControlTypeTextImages
  23. };
  24. /**
  25. 单个控件宽度
  26. - HMSegmentedControlSegmentWidthStyleFixed: 1、当控件总宽度小于等于父视图宽度时,单个控件宽度等于均等分父视图宽度;2、当控件总宽度大于父视图宽度时,取最大宽度控件的宽作为单个控件的宽度
  27. - HMSegmentedControlSegmentWidthStyleDynamic: 控件的宽度等于文字或者图片的最大宽度
  28. - HMSegmentedControlSegmentWidthStyleDynamicFixedSuperView: 1、当控件总宽度小于等于父视图宽度时,单个控件宽度等于均等分父视图宽度;2、当控件总宽度大于父视图宽度时,控件的宽度等于文字或者图片的最大宽度。注意:当前只对Segmented类型为HMSegmentedControlTypeText的进行了适配
  29. */
  30. typedef NS_ENUM(NSInteger, HMSegmentedControlSegmentWidthStyle)
  31. {
  32. HMSegmentedControlSegmentWidthStyleFixed,
  33. HMSegmentedControlSegmentWidthStyleDynamic,
  34. HMSegmentedControlSegmentWidthStyleDynamicFixedSuperView,
  35. };
  36. /**
  37. 当Segmented类型为HMSegmentedControlTypeTextImages时,图片相对于文字的位置
  38. - HMSegmentedControlImagePositionBehindText: 图片在文字的背后
  39. - HMSegmentedControlImagePositionLeftOfText: 图片在文字的左边
  40. - HMSegmentedControlImagePositionRightOfText: 图片在文字的右边
  41. - HMSegmentedControlImagePositionAboveText: 图片在文字的上边
  42. - HMSegmentedControlImagePositionBelowText: 图片在文字的下边
  43. */
  44. typedef NS_ENUM(NSInteger, HMSegmentedControlImagePosition)
  45. {
  46. HMSegmentedControlImagePositionBehindText,
  47. HMSegmentedControlImagePositionLeftOfText,
  48. HMSegmentedControlImagePositionRightOfText,
  49. HMSegmentedControlImagePositionAboveText,
  50. HMSegmentedControlImagePositionBelowText
  51. };
  52. /**
  53. 下标类型以及宽度是否充满当前控件
  54. - HMSegmentedControlSelectionStyleTextWidthStripe: 下标为下划线类型,并且宽度等于文字的宽度
  55. - HMSegmentedControlSelectionStyleFullWidthStripe: 下标为下划线类型,并且宽度等于当前控件的宽度
  56. - HMSegmentedControlSelectionStyleBox: 没有下标,一个矩形框
  57. - HMSegmentedControlSelectionStyleArrow: 向下箭头
  58. */
  59. typedef NS_ENUM(NSInteger, HMSegmentedControlSelectionStyle)
  60. {
  61. HMSegmentedControlSelectionStyleTextWidthStripe,
  62. HMSegmentedControlSelectionStyleFullWidthStripe,
  63. HMSegmentedControlSelectionStyleBox,
  64. HMSegmentedControlSelectionStyleArrow
  65. };
  66. /**
  67. 下标位置
  68. - HMSegmentedControlSelectionIndicatorLocationUp: 位于控件上方
  69. - HMSegmentedControlSelectionIndicatorLocationDown: 位于控件下方
  70. - HMSegmentedControlSelectionIndicatorLocationNone: 无下标
  71. */
  72. typedef NS_ENUM(NSInteger, HMSegmentedControlSelectionIndicatorLocation)
  73. {
  74. HMSegmentedControlSelectionIndicatorLocationUp,
  75. HMSegmentedControlSelectionIndicatorLocationDown,
  76. HMSegmentedControlSelectionIndicatorLocationNone // No selection indicator
  77. };
  78. /**
  79. 边框类型
  80. - HMSegmentedControlBorderTypeNone: 无边框
  81. - HMSegmentedControlBorderTypeTop: 上面有边框
  82. - HMSegmentedControlBorderTypeLeft: 左边有边框
  83. - HMSegmentedControlBorderTypeBottom: 下面有边框
  84. - HMSegmentedControlBorderTypeRight: 右边有边框
  85. */
  86. typedef NS_OPTIONS(NSInteger, HMSegmentedControlBorderType)
  87. {
  88. HMSegmentedControlBorderTypeNone = 0,
  89. HMSegmentedControlBorderTypeTop = (1 << 0),
  90. HMSegmentedControlBorderTypeLeft = (1 << 1),
  91. HMSegmentedControlBorderTypeBottom = (1 << 2),
  92. HMSegmentedControlBorderTypeRight = (1 << 3)
  93. };
  94. enum
  95. {
  96. HMSegmentedControlNoSegment = -1 // Segment index for no selected segment
  97. };
  98. @interface HMSegmentedControl : UIControl
  99. /**
  100. 标题
  101. */
  102. @property (nonatomic, strong) NSArray<NSString *> *sectionTitles;
  103. /**
  104. 图片
  105. */
  106. @property (nonatomic, strong) NSArray<UIImage *> *sectionImages;
  107. /**
  108. 选中图片
  109. */
  110. @property (nonatomic, strong) NSArray<UIImage *> *sectionSelectedImages;
  111. /**
  112. Provide a block to be executed when selected index is changed.
  113. Alternativly, you could use `addTarget:action:forControlEvents:`
  114. */
  115. @property (nonatomic, copy) IndexChangeBlock indexChangeBlock;
  116. /**
  117. Used to apply custom text styling to titles when set.
  118. When this block is set, no additional styling is applied to the `NSAttributedString` object returned from this block.
  119. */
  120. @property (nonatomic, copy) HMTitleFormatterBlock titleFormatter;
  121. /**
  122. Text attributes to apply to item title text.
  123. */
  124. @property (nonatomic, strong) NSDictionary *titleTextAttributes UI_APPEARANCE_SELECTOR;
  125. /*
  126. Text attributes to apply to selected item title text.
  127. Attributes not set in this dictionary are inherited from `titleTextAttributes`.
  128. */
  129. @property (nonatomic, strong) NSDictionary *selectedTitleTextAttributes UI_APPEARANCE_SELECTOR;
  130. /**
  131. Segmented control background color.
  132. Default is `[UIColor whiteColor]`
  133. */
  134. @property (nonatomic, strong) UIColor *backgroundColor UI_APPEARANCE_SELECTOR;
  135. /**
  136. Color for the selection indicator stripe
  137. Default is `R:52, G:181, B:229`
  138. */
  139. @property (nonatomic, strong) UIColor *selectionIndicatorColor UI_APPEARANCE_SELECTOR;
  140. /**
  141. Color for the selection indicator box
  142. Default is selectionIndicatorColor
  143. */
  144. @property (nonatomic, strong) UIColor *selectionIndicatorBoxColor UI_APPEARANCE_SELECTOR;
  145. /**
  146. Color for the vertical divider between segments.
  147. Default is `[UIColor blackColor]`
  148. */
  149. @property (nonatomic, strong) UIColor *verticalDividerColor UI_APPEARANCE_SELECTOR;
  150. /**
  151. Opacity for the seletion indicator box.
  152. Default is `0.2f`
  153. */
  154. @property (nonatomic) CGFloat selectionIndicatorBoxOpacity;
  155. /**
  156. Width the vertical divider between segments that is added when `verticalDividerEnabled` is set to YES.
  157. Default is `1.0f`
  158. */
  159. @property (nonatomic, assign) CGFloat verticalDividerWidth;
  160. /**
  161. Specifies the style of the control
  162. Default is `HMSegmentedControlTypeText`
  163. */
  164. @property (nonatomic, assign) HMSegmentedControlType type;
  165. /**
  166. Specifies the style of the selection indicator.
  167. Default is `HMSegmentedControlSelectionStyleTextWidthStripe`
  168. */
  169. @property (nonatomic, assign) HMSegmentedControlSelectionStyle selectionStyle;
  170. /**
  171. Specifies the style of the segment's width.
  172. Default is `HMSegmentedControlSegmentWidthStyleFixed`
  173. */
  174. @property (nonatomic, assign) HMSegmentedControlSegmentWidthStyle segmentWidthStyle;
  175. /**
  176. Specifies the location of the selection indicator.
  177. Default is `HMSegmentedControlSelectionIndicatorLocationUp`
  178. */
  179. @property (nonatomic, assign) HMSegmentedControlSelectionIndicatorLocation selectionIndicatorLocation;
  180. /*
  181. Specifies the border type.
  182. Default is `HMSegmentedControlBorderTypeNone`
  183. */
  184. @property (nonatomic, assign) HMSegmentedControlBorderType borderType;
  185. /**
  186. Specifies the image position relative to the text. Only applicable for HMSegmentedControlTypeTextImages
  187. Default is `HMSegmentedControlImagePositionBehindText`
  188. */
  189. @property (nonatomic) HMSegmentedControlImagePosition imagePosition;
  190. /**
  191. Specifies the image position relative to the text. Only applicable for HMSegmentedControlTypeTextImages
  192. Default is `HMSegmentedControlImagePositionBehindText`
  193. */
  194. @property (nonatomic) CGSize imageSize;
  195. /**
  196. Specifies the distance between the text and the image. Only applicable for HMSegmentedControlTypeTextImages
  197. Default is `0,0`
  198. */
  199. @property (nonatomic) CGFloat textImageSpacing;
  200. /**
  201. Specifies the border color.
  202. Default is `[UIColor blackColor]`
  203. */
  204. @property (nonatomic, strong) UIColor *borderColor;
  205. /**
  206. Specifies the border width.
  207. Default is `1.0f`
  208. */
  209. @property (nonatomic, assign) CGFloat borderWidth;
  210. /**
  211. Default is YES. Set to NO to deny scrolling by dragging the scrollView by the user.
  212. */
  213. @property(nonatomic, getter = isUserDraggable) BOOL userDraggable;
  214. /**
  215. Default is YES. Set to NO to deny any touch events by the user.
  216. */
  217. @property(nonatomic, getter = isTouchEnabled) BOOL touchEnabled;
  218. /**
  219. Default is NO. Set to YES to show a vertical divider between the segments.
  220. */
  221. @property(nonatomic, getter = isVerticalDividerEnabled) BOOL verticalDividerEnabled;
  222. /**
  223. 当控件总宽度小于父视图宽度时,是否需要占满父视图的宽度,默认为“否”。注意:当前只对HMSegmentedControlTypeTextImages有效
  224. */
  225. @property (nonatomic, assign) BOOL shouldStretchSegmentsToSuperViewSize;
  226. /**
  227. Index of the currently selected segment.
  228. */
  229. @property (nonatomic, assign) NSInteger selectedSegmentIndex;
  230. /**
  231. Height of the selection indicator. Only effective when `HMSegmentedControlSelectionStyle` is either `HMSegmentedControlSelectionStyleTextWidthStripe` or `HMSegmentedControlSelectionStyleFullWidthStripe`.
  232. Default is 5.0
  233. */
  234. @property (nonatomic, readwrite) CGFloat selectionIndicatorHeight;
  235. /**
  236. Edge insets for the selection indicator.
  237. NOTE: This does not affect the bounding box of HMSegmentedControlSelectionStyleBox
  238. When HMSegmentedControlSelectionIndicatorLocationUp is selected, bottom edge insets are not used
  239. When HMSegmentedControlSelectionIndicatorLocationDown is selected, top edge insets are not used
  240. Defaults are top: 0.0f
  241. left: 0.0f
  242. bottom: 0.0f
  243. right: 0.0f
  244. */
  245. @property (nonatomic, readwrite) UIEdgeInsets selectionIndicatorEdgeInsets;
  246. /**
  247. Inset left and right edges of segments.
  248. Default is UIEdgeInsetsMake(0, 5, 0, 5)
  249. */
  250. @property (nonatomic, readwrite) UIEdgeInsets segmentEdgeInset;
  251. @property (nonatomic, readwrite) UIEdgeInsets enlargeEdgeInset;
  252. /**
  253. Default is YES. Set to NO to disable animation during user selection.
  254. */
  255. @property (nonatomic) BOOL shouldAnimateUserSelection;
  256. - (id)initWithSectionTitles:(NSArray<NSString *> *)sectiontitles;
  257. - (id)initWithSectionImages:(NSArray<UIImage *> *)sectionImages sectionSelectedImages:(NSArray<UIImage *> *)sectionSelectedImages;
  258. - (instancetype)initWithSectionImages:(NSArray<UIImage *> *)sectionImages sectionSelectedImages:(NSArray<UIImage *> *)sectionSelectedImages titlesForSections:(NSArray<NSString *> *)sectiontitles;
  259. - (void)setSelectedSegmentIndex:(NSUInteger)index animated:(BOOL)animated;
  260. - (void)setIndexChangeBlock:(IndexChangeBlock)indexChangeBlock;
  261. - (void)setTitleFormatter:(HMTitleFormatterBlock)titleFormatter;
  262. @end