SPAlertController.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // SPAlertController.h
  3. // SPAlertController
  4. //
  5. // Created by 乐升平 on 18/10/12. https://github.com/SPStore/SPAlertController
  6. // Copyright © 2018-2019 leshengping (lesp163@163.com). All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. typedef NS_ENUM(NSInteger, SPAlertControllerStyle) {
  11. SPAlertControllerStyleActionSheet = 0, // 从单侧弹出(顶/左/底/右)
  12. SPAlertControllerStyleAlert, // 从中间弹出
  13. };
  14. typedef NS_ENUM(NSInteger, SPAlertAnimationType) {
  15. SPAlertAnimationTypeDefault = 0, // 默认动画,如果是SPAlertControllerStyleActionSheet样式,默认动画等效于SPAlertAnimationTypeFromBottom,如果是SPAlertControllerStyleAlert样式,默认动画等效于SPAlertAnimationTypeShrink
  16. SPAlertAnimationTypeFromBottom, // 从底部弹出
  17. SPAlertAnimationTypeFromTop, // 从顶部弹出
  18. SPAlertAnimationTypeFromRight, // 从右边弹出
  19. SPAlertAnimationTypeFromLeft, // 从左边弹出
  20. SPAlertAnimationTypeShrink, // 收缩动画
  21. SPAlertAnimationTypeExpand, // 发散动画
  22. SPAlertAnimationTypeFade, // 渐变动画
  23. SPAlertAnimationTypeNone, // 无动画
  24. SPAlertAnimationTypeAlpha NS_ENUM_DEPRECATED_IOS(8_0, 8_0, "Use SPAlertAnimationTypeFade instead"), // 渐变动画
  25. SPAlertAnimationTypeRaiseUp NS_ENUM_DEPRECATED_IOS(8_0, 8_0, "Use SPAlertAnimationTypeFromBottom instead"), // 从底部弹出
  26. SPAlertAnimationTypeDropDown NS_ENUM_DEPRECATED_IOS(8_0, 8_0, "Use SPAlertAnimationTypeFromTop instead"), // 从顶部弹出
  27. };
  28. typedef NS_ENUM(NSInteger, SPAlertActionStyle) {
  29. SPAlertActionStyleDefault = 0, // 默认样式
  30. SPAlertActionStyleCancel, // 取消样式,字体加粗
  31. SPAlertActionStyleDestructive // 红色字体样式
  32. };
  33. // ===================================================== SPAlertAction =====================================================
  34. @interface SPAlertAction : NSObject <NSCopying>
  35. + (instancetype)actionWithTitle:(nullable NSString *)title style:(SPAlertActionStyle)style handler:(void (^ __nullable)(SPAlertAction *action))handler;
  36. /** action的标题 */
  37. @property(nullable, nonatomic, copy) NSString *title;
  38. /** action的富文本标题 */
  39. @property(nullable, nonatomic, copy) NSAttributedString *attributedTitle;
  40. /** action的图标,位于title的左边 */
  41. @property(nullable, nonatomic, copy) UIImage *image;
  42. /** title跟image之间的间距 */
  43. @property (nonatomic, assign) CGFloat imageTitleSpacing;
  44. /** 渲染颜色,当外部的图片使用了UIImageRenderingModeAlwaysTemplate时,使用该属性可改变图片的颜色 */
  45. @property (nonatomic, strong) UIColor *tintColor;
  46. /** 是否能点击,默认为YES */
  47. @property(nonatomic, getter=isEnabled) BOOL enabled;
  48. /** action的标题颜色,这个颜色只是普通文本的颜色,富文本颜色需要用NSForegroundColorAttributeName */
  49. @property(nonatomic, strong) UIColor *titleColor;
  50. /** action的标题字体,如果文字太长显示不全,会自动改变字体自适应按钮宽度,最多压缩文字为原来的0.5倍封顶 */
  51. @property(nonatomic, strong) UIFont *titleFont;
  52. /** action的标题的内边距,如果在不改变字体的情况下想增大action的高度,可以设置该属性的top和bottom值,默认UIEdgeInsetsMake(0, 15, 0, 15) */
  53. @property(nonatomic, assign) UIEdgeInsets titleEdgeInsets;
  54. /** 样式 */
  55. @property(nonatomic, readonly) SPAlertActionStyle style;
  56. @end
  57. // ===================================================== SPAlertController =====================================================
  58. @protocol SPAlertControllerDelegate;
  59. @interface SPAlertController : UIViewController
  60. + (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle;
  61. + (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
  62. - (void)addAction:(SPAlertAction *)action;
  63. @property (nonatomic, readonly) NSArray<SPAlertAction *> *actions;
  64. /* 添加文本输入框
  65. * 一旦添加后就会回调一次(仅回调一次,因此可以在这个block块里面自由定制textFiled,如设置textField的属性,设置代理,添加addTarget,监听通知等);
  66. */
  67. - (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
  68. @property(nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;
  69. /** 主标题 */
  70. @property(nullable, nonatomic, copy) NSString *title;
  71. /** 副标题 */
  72. @property(nullable, nonatomic, copy) NSString *message;
  73. /** 主标题(富文本) */
  74. @property(nullable, nonatomic, copy) NSAttributedString *attributedTitle;
  75. /** 副标题(富文本) */
  76. @property(nullable, nonatomic, copy) NSAttributedString *attributedMessage;
  77. /** 头部图标,位置处于title之上,大小取决于图片本身大小 */
  78. @property(nullable,nonatomic, copy) UIImage *image;
  79. /** 主标题颜色 */
  80. @property(nonatomic, strong) UIColor *titleColor;
  81. /** 主标题字体,默认18,加粗 */
  82. @property(nonatomic, strong) UIFont *titleFont;
  83. /** 副标题颜色 */
  84. @property(nonatomic, strong) UIColor *messageColor;
  85. /** 副标题字体,默认16,未加粗 */
  86. @property(nonatomic, strong) UIFont *messageFont;
  87. /** 对齐方式(包括主标题和副标题) */
  88. @property(nonatomic, assign) NSTextAlignment textAlignment;
  89. /** 头部图标的限制大小,默认无穷大 */
  90. @property (nonatomic, assign) CGSize imageLimitSize;
  91. /** 图片的tintColor,当外部的图片使用了UIImageRenderingModeAlwaysTemplate时,该属性可起到作用 */
  92. @property (nonatomic, strong) UIColor *imageTintColor;
  93. /*
  94. * action水平排列还是垂直排列
  95. * actionSheet样式下:默认为UILayoutConstraintAxisVertical(垂直排列), 如果设置为UILayoutConstraintAxisHorizontal(水平排列),则除去取消样式action之外的其余action将水平排列
  96. * alert样式下:当actions的个数大于2,或者某个action的title显示不全时为UILayoutConstraintAxisVertical(垂直排列),否则默认为UILayoutConstraintAxisHorizontal(水平排列),此样式下设置该属性可以修改所有action的排列方式
  97. * 不论哪种样式,只要外界设置了该属性,永远以外界设置的优先
  98. */
  99. @property(nonatomic) UILayoutConstraintAxis actionAxis;
  100. /* 距离屏幕边缘的最小间距
  101. * alert样式下该属性是指对话框四边与屏幕边缘之间的距离,此样式下默认值随设备变化,actionSheet样式下是指弹出边的对立边与屏幕之间的距离,比如如果从右边弹出,那么该属性指的就是对话框左边与屏幕之间的距离,此样式下默认值为70
  102. */
  103. @property(nonatomic, assign) CGFloat minDistanceToEdges;
  104. /** SPAlertControllerStyleAlert样式下默认6.0f,SPAlertControllerStyleActionSheet样式下默认13.0f,去除半径设置为0即可 */
  105. @property(nonatomic, assign) CGFloat cornerRadius;
  106. /** 对话框的偏移量,y值为正向下偏移,为负向上偏移;x值为正向右偏移,为负向左偏移,该属性只对SPAlertControllerStyleAlert样式有效,键盘的frame改变会自动偏移,如果手动设置偏移只会取手动设置的 */
  107. @property(nonatomic, assign) CGPoint offsetForAlert;
  108. /** 设置alert样式下的偏移量,动画为NO则跟属性offsetForAlert等效 */
  109. - (void)setOffsetForAlert:(CGPoint)offsetForAlert animated:(BOOL)animated;
  110. /** 是否需要对话框拥有毛玻璃,默认为YES */
  111. @property(nonatomic, assign) BOOL needDialogBlur;
  112. /** 是否单击背景退出对话框,默认为YES */
  113. @property(nonatomic, assign) BOOL tapBackgroundViewDismiss;
  114. @property(nonatomic, weak) id<SPAlertControllerDelegate> delegate;
  115. @property(nonatomic, readonly) SPAlertControllerStyle preferredStyle;
  116. @property(nonatomic, readonly) SPAlertAnimationType animationType;
  117. /** 设置action与下一个action之间的间距, action仅限于非取消样式,必须在'-addAction:'之后设置,iOS11或iOS11以上才能使用 */
  118. - (void)setCustomSpacing:(CGFloat)spacing afterAction:(SPAlertAction *)action API_AVAILABLE(ios(11.0));
  119. - (CGFloat)customSpacingAfterAction:(SPAlertAction *)action API_AVAILABLE(ios(11.0));
  120. /** 设置蒙层的外观样式,可通过alpha调整透明度 */
  121. - (void)setBackgroundViewAppearanceStyle:(UIBlurEffectStyle)style alpha:(CGFloat)alpha;
  122. // 插入一个组件view,位置处于头部和action部分之间,要求头部和action部分同时存在
  123. - (void)insertComponentView:(nonnull UIView *)componentView;
  124. // ---------------------------------------------- custom -----------------------------------------------------
  125. /**
  126. 创建控制器(自定义整个对话框)
  127. @param customAlertView 整个对话框的自定义view
  128. @param preferredStyle 对话框样式
  129. @param animationType 动画类型
  130. @return 控制器对象
  131. */
  132. + (instancetype)alertControllerWithCustomAlertView:(nonnull UIView *)customAlertView preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
  133. /**
  134. 创建控制器(自定义对话框的头部)
  135. @param customHeaderView 头部自定义view
  136. @param preferredStyle 对话框样式
  137. @param animationType 动画类型
  138. @return 控制器对象
  139. */
  140. + (instancetype)alertControllerWithCustomHeaderView:(nonnull UIView *)customHeaderView preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
  141. /**
  142. 创建控制器(自定义对话框的action部分)
  143. @param customActionSequenceView action部分的自定义view
  144. @param title 大标题
  145. @param message 副标题
  146. @param preferredStyle 对话框样式
  147. @param animationType 动画类型
  148. @return 控制器对象
  149. */
  150. + (instancetype)alertControllerWithCustomActionSequenceView:(nonnull UIView *)customActionSequenceView title:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
  151. /** 更新自定义view的size,比如屏幕旋转,自定义view的大小发生了改变,可通过该方法更新size */
  152. - (void)updateCustomViewSize:(CGSize)size;
  153. @property(nonatomic, assign) CGFloat cornerRadiusForAlert NS_DEPRECATED_IOS(8_0, 8_0,"Use cornerRadius instead");
  154. @property (nonatomic, assign) CGFloat maxTopMarginForActionSheet NS_DEPRECATED_IOS(8_0, 8_0,"Use minDistanceToEdges instead");
  155. @property(nonatomic, assign) CGFloat maxMarginForAlert NS_DEPRECATED_IOS(8_0, 8_0,"Use minDistanceToEdges instead");
  156. @property(nonatomic, assign) NSInteger maxNumberOfActionHorizontalArrangementForAlert NS_DEPRECATED_IOS(8_0, 8_0,"Use actionAxis instead");
  157. @property(nonatomic, assign) CGFloat offsetYForAlert NS_DEPRECATED_IOS(8_0, 8_0,"Use offsetForAlert instead");
  158. + (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType customView:(nullable UIView *)customView NS_DEPRECATED_IOS(8_0, 8_0,"Use +alertControllerWithCustomAlertView:preferredStyle:animationType:");
  159. + (instancetype)alertControllerWithPreferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType customHeaderView:(nullable UIView *)customHeaderView NS_DEPRECATED_IOS(8_0, 8_0,"Use +alertControllerWithCustomHeaderView:preferredStyle:animationType:");
  160. + (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType customCenterView:(nullable UIView *)customCenterView NS_DEPRECATED_IOS(8_0, 8_0,"Use -insertComponentView:");
  161. + (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType customFooterView:(nullable UIView *)customFooterView NS_DEPRECATED_IOS(8_0, 8_0,"Use +alertControllerWithCustomActionSequenceView:title:message:preferredStyle:animationType:");
  162. @end
  163. @protocol SPAlertControllerDelegate <NSObject>
  164. @optional;
  165. - (void)willPresentAlertController:(SPAlertController *)alertController; // 将要present
  166. - (void)didPresentAlertController:(SPAlertController *)alertController; // 已经present
  167. - (void)willDismissAlertController:(SPAlertController *)alertController; // 将要dismiss
  168. - (void)didDismissAlertController:(SPAlertController *)alertController; // 已经dismiss
  169. - (void)sp_alertControllerWillShow:(SPAlertController *)alertController NS_DEPRECATED_IOS(8_0, 8_0,"Use -willPresentAlertController:");
  170. - (void)sp_alertControllerDidShow:(SPAlertController *)alertController NS_DEPRECATED_IOS(8_0, 8_0,"Use -DidPresentAlertController:");
  171. - (void)sp_alertControllerWillHide:(SPAlertController *)alertController NS_DEPRECATED_IOS(8_0, 8_0,"Use -willDismissAlertController:");
  172. - (void)sp_alertControllerDidHide:(SPAlertController *)alertController NS_DEPRECATED_IOS(8_0, 8_0,"Use -DidDismissAlertController:");
  173. @end
  174. @interface SPAlertPresentationController : UIPresentationController
  175. @end
  176. @interface SPAlertAnimation : NSObject <UIViewControllerAnimatedTransitioning>
  177. + (instancetype)animationIsPresenting:(BOOL)presenting;
  178. @end
  179. NS_ASSUME_NONNULL_END