-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathSPAlertController.h
More file actions
executable file
·275 lines (208 loc) · 11.1 KB
/
SPAlertController.h
File metadata and controls
executable file
·275 lines (208 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
// SPAlertController.h
// SPAlertController
//
// Created by 乐升平 on 18/10/12. https://github.com/SPStore/SPAlertController
// Copyright © 2018-2019 leshengping (lesp163@163.com). All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, SPAlertControllerStyle) {
SPAlertControllerStyleActionSheet = 0, // 从底部弹出
SPAlertControllerStyleAlert, // 从中间弹出
};
typedef NS_ENUM(NSInteger, SPAlertAnimationType) {
// 如果是SPAlertControllerStyleActionSheet样式,默认动画等效于SPAlertAnimationTypeFromBottom
// 如果是SPAlertControllerStyleAlert样式,默认动画等效于SPAlertAnimationTypeShrink
SPAlertAnimationTypeDefault = 0,
// 方向动画
SPAlertAnimationTypeFromBottom, // 从底部弹出
SPAlertAnimationTypeFromTop, // 从顶部弹出
// 效果动画
SPAlertAnimationTypeShrink, // 收缩动画
SPAlertAnimationTypeExpand, // 发散动画
SPAlertAnimationTypeFade, // 渐变动画
SPAlertAnimationTypeNone, // 无动画
SPAlertAnimationTypeFromRight NS_ENUM_DEPRECATED_IOS(8_0, 26_0, "This enum value will be deprecated in a future version"), // 从右边弹出
SPAlertAnimationTypeFromLeft NS_ENUM_DEPRECATED_IOS(8_0, 26_0, "This enum value will be deprecated in a future version"), // 从左边弹出
};
typedef NS_ENUM(NSInteger, SPAlertActionStyle) {
SPAlertActionStyleDefault = 0, // 默认样式
SPAlertActionStyleCancel, // 取消样式,字体加粗
SPAlertActionStyleDestructive // 红色字体样式
};
// =====================================================
// SPAlertAction
// =====================================================
@interface SPAlertAction : NSObject <NSCopying>
+ (instancetype)actionWithTitle:(nullable NSString *)title style:(SPAlertActionStyle)style handler:(void (^ __nullable)(SPAlertAction *action))handler;
/** action的标题 */
@property(nullable, nonatomic, copy) NSString *title;
/** action的富文本标题 */
@property(nullable, nonatomic, copy) NSAttributedString *attributedTitle;
/** action的图标,位于title的左边 */
@property(nullable, nonatomic, copy) UIImage *image;
/** title跟image之间的间距 */
@property (nonatomic, assign) CGFloat imageTitleSpacing;
/** 渲染颜色,当外部的图片使用了UIImageRenderingModeAlwaysTemplate时,使用该属性可改变图片的颜色 */
@property (nonatomic, strong) UIColor *tintColor;
/** 是否能点击,默认为YES */
@property(nonatomic, getter=isEnabled) BOOL enabled;
/** action的标题颜色,这个颜色只是普通文本的颜色,富文本颜色需要用NSForegroundColorAttributeName */
@property(nonatomic, strong) UIColor *titleColor;
/** action的背景颜色 */
@property(nonatomic, strong) UIColor *backgroundColor;
/** action的标题字体,如果文字太长显示不全,会自动改变字体自适应按钮宽度,最多压缩文字为原来的0.5倍封顶 */
@property(nonatomic, strong) UIFont *titleFont;
/** action的标题的内边距,如果在不改变字体的情况下想增大action的高度,可以设置该属性的top和bottom值,默认UIEdgeInsetsMake(0, 15, 0, 15) */
@property(nonatomic, assign) UIEdgeInsets titleEdgeInsets;
/** 样式 */
@property(nonatomic, readonly) SPAlertActionStyle style;
/** 点击后是否自动dismiss对话框,默认为YES */
@property(nonatomic, assign) BOOL allowsAutoDismiss;
@end
// =====================================================
// SPAlertController
// =====================================================
@protocol SPAlertControllerDelegate;
@interface SPAlertController : UIViewController
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle;
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
+ (instancetype)alertControllerWithCustomHeaderView:(nonnull UIView *)customHeaderView preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
- (void)addAction:(SPAlertAction *)action;
@property (nonatomic, readonly) NSArray<SPAlertAction *> *actions;
/**
* 添加文本输入框
* 一旦添加后就会回调一次(仅回调一次,因此可以在这个block块里面自由定制textFiled,如设置textField的属性,设置代理,添加addTarget,监听通知等)
*/
- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
@property(nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;
/** 主标题 */
@property(nullable, nonatomic, copy) NSString *title;
/** 副标题 */
@property(nullable, nonatomic, copy) NSString *message;
/** 主标题(富文本) */
@property(nullable, nonatomic, copy) NSAttributedString *attributedTitle;
/** 副标题(富文本) */
@property(nullable, nonatomic, copy) NSAttributedString *attributedMessage;
/** 头部图标,位置处于title之上,大小取决于图片本身大小 */
@property(nullable,nonatomic, copy) UIImage *image;
/** 主标题颜色 */
@property(nonatomic, strong) UIColor *titleColor;
/** 主标题字体,默认18,加粗 */
@property(nonatomic, strong) UIFont *titleFont;
/** 副标题颜色 */
@property(nonatomic, strong) UIColor *messageColor;
/** 副标题字体,默认16,未加粗 */
@property(nonatomic, strong) UIFont *messageFont;
/** 对齐方式(包括主标题和副标题) */
@property(nonatomic, assign) NSTextAlignment textAlignment;
/** 头部图标的限制大小,默认无穷大 */
@property (nonatomic, assign) CGSize imageLimitSize;
/** 图片的tintColor,当外部的图片使用了UIImageRenderingModeAlwaysTemplate时,该属性可起到作用 */
@property (nonatomic, strong) UIColor *imageTintColor;
/**
* action水平排列还是垂直排列
*
* actionSheet样式下:
* 默认为UILayoutConstraintAxisVertical(垂直排列)
* 如果设置为UILayoutConstraintAxisHorizontal(水平排列),则除去取消样式action之外的其余action将水平排列
*
* alert样式下:
* 当actions的个数大于2,或者某个action的title显示不全时为UILayoutConstraintAxisVertical(垂直排列)
* 否则默认为UILayoutConstraintAxisHorizontal(水平排列),此样式下设置该属性可以修改所有action的排列方式
*/
@property(nonatomic) UILayoutConstraintAxis actionAxis;
/**
* 距离屏幕边缘的最小间距
*
* alert样式下:
* 该属性是指对话框四边与屏幕边缘之间的距离,此样式下默认值随设备变化
*
* actionSheet样式下:
* 是指弹出边的对立边与屏幕之间的距离
* 比如如果从右边弹出,那么该属性指的就是对话框左边与屏幕之间的距离,此样式下默认值为70
*/
@property(nonatomic, assign) CGFloat minDistanceToEdges;
/**
* 圆角半径
* SPAlertControllerStyleAlert样式下默认6.0f
* SPAlertControllerStyleActionSheet样式下默认13.0f
* 去除半径设置为0即可
*/
@property(nonatomic, assign) CGFloat cornerRadius;
/**
* 对话框的偏移量
* y值为正向下偏移,为负向上偏移
* x值为正向右偏移,为负向左偏移
* 该属性只对SPAlertControllerStyleAlert样式有效
* 键盘的frame改变会自动偏移,如果手动设置偏移只会取手动设置的
*/
@property(nonatomic, assign) CGPoint offsetForAlert;
/** 设置alert样式下的偏移量,动画为NO则跟属性offsetForAlert等效 */
- (void)setOffsetForAlert:(CGPoint)offsetForAlert animated:(BOOL)animated;
/** 使用键盘动画参数调整视图位置 */
- (void)makeViewOffsetWithDuration:(NSTimeInterval)duration curve:(UIViewAnimationCurve)curve;
/** 是否需要对话框拥有毛玻璃,默认为YES */
@property(nonatomic, assign) BOOL needDialogBlur;
/** 是否单击背景退出对话框,默认为YES */
@property(nonatomic, assign) BOOL tapBackgroundViewDismiss;
@property(nonatomic, weak) id<SPAlertControllerDelegate> delegate;
@property(nonatomic, readonly) SPAlertControllerStyle preferredStyle;
@property(nonatomic, readonly) SPAlertAnimationType animationType;
/** 设置action与下一个action之间的间距, action仅限于非取消样式,必须在'-addAction:'之后设置,iOS11或iOS11以上才能使用 */
- (void)setCustomSpacing:(CGFloat)spacing afterAction:(SPAlertAction *)action API_AVAILABLE(ios(11.0));
- (CGFloat)customSpacingAfterAction:(SPAlertAction *)action API_AVAILABLE(ios(11.0));
/** 设置蒙层的外观样式,可通过alpha调整透明度 */
- (void)setBackgroundViewAppearanceStyle:(UIBlurEffectStyle)style alpha:(CGFloat)alpha;
// 插入一个组件view,位置处于头部和action部分之间,要求头部和action部分同时存在
- (void)insertComponentView:(nonnull UIView *)componentView;
// ----------------------------------------------
// 自定义视图
// ----------------------------------------------
/**
* 创建控制器(自定义整个对话框)
*
* @param customAlertView 整个对话框的自定义view
* @param preferredStyle 对话框样式
* @param animationType 动画类型
* @return 控制器对象
* @warning 此方法将在未来版本中移除
*/
+ (instancetype)alertControllerWithCustomAlertView:(nonnull UIView *)customAlertView preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType NS_DEPRECATED_IOS(8_0, 26_0, "This API will be deprecated in a future version");
/**
* 创建控制器(自定义对话框的action部分)
*
* @param customActionSequenceView action部分的自定义view
* @param title 大标题
* @param message 副标题
* @param preferredStyle 对话框样式
* @param animationType 动画类型
* @return 控制器对象
* @warning 此方法将在未来版本中移除
*/
+ (instancetype)alertControllerWithCustomActionSequenceView:(nonnull UIView *)customActionSequenceView title:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType NS_DEPRECATED_IOS(8_0, 26_0, "This API will be deprecated in a future version");
/**
* 更新自定义view的size
* 比如屏幕旋转,自定义view的大小发生了改变,可通过该方法更新size
* @warning 此方法将在未来版本中移除
*/
- (void)updateCustomViewSize:(CGSize)size NS_DEPRECATED_IOS(8_0, 26_0, "This API will be deprecated in a future version");
@end
@protocol SPAlertControllerDelegate <NSObject>
@optional;
/** 将要present */
- (void)willPresentAlertController:(SPAlertController *)alertController;
/** 已经present */
- (void)didPresentAlertController:(SPAlertController *)alertController;
/** 将要dismiss */
- (void)willDismissAlertController:(SPAlertController *)alertController;
/** 已经dismiss */
- (void)didDismissAlertController:(SPAlertController *)alertController;
@end
@interface SPAlertPresentationController : UIPresentationController
@end
@interface SPAlertAnimation : NSObject <UIViewControllerAnimatedTransitioning>
+ (instancetype)animationIsPresenting:(BOOL)presenting;
@end
NS_ASSUME_NONNULL_END