forked from Tencent/QMUI_iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQMUIButton.h
More file actions
368 lines (301 loc) · 18.3 KB
/
QMUIButton.h
File metadata and controls
368 lines (301 loc) · 18.3 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// QMUIButton.h
// qmui
//
// Created by MoLice on 14-7-7.
// Copyright (c) 2014年 QMUI Team. All rights reserved.
//
#import <UIKit/UIKit.h>
/// 控制图片在UIButton里的位置,默认为QMUIButtonImagePositionLeft
typedef NS_ENUM(NSUInteger, QMUIButtonImagePosition) {
QMUIButtonImagePositionTop, // imageView在titleLabel上面
QMUIButtonImagePositionLeft, // imageView在titleLabel左边
QMUIButtonImagePositionBottom, // imageView在titleLabel下面
QMUIButtonImagePositionRight, // imageView在titleLabel右边
};
typedef NS_ENUM(NSUInteger, QMUIGhostButtonColor) {
QMUIGhostButtonColorBlue,
QMUIGhostButtonColorRed,
QMUIGhostButtonColorGreen,
QMUIGhostButtonColorGray,
QMUIGhostButtonColorWhite,
};
typedef NS_ENUM(NSUInteger, QMUIFillButtonColor) {
QMUIFillButtonColorBlue,
QMUIFillButtonColorRed,
QMUIFillButtonColorGreen,
QMUIFillButtonColorGray,
QMUIFillButtonColorWhite,
};
typedef NS_ENUM(NSUInteger, QMUINavigationButtonType) {
QMUINavigationButtonTypeNormal, // 普通导航栏文字按钮
QMUINavigationButtonTypeBold, // 导航栏加粗按钮
QMUINavigationButtonTypeImage, // 图标按钮
QMUINavigationButtonTypeBack // 自定义返回按钮(可以同时带有title)
};
typedef NS_ENUM(NSUInteger, QMUIToolbarButtonType) {
QMUIToolbarButtonTypeNormal, // 普通工具栏按钮
QMUIToolbarButtonTypeRed, // 工具栏红色按钮,用于删除等警告性操作
QMUIToolbarButtonTypeImage, // 图标类型的按钮
};
typedef NS_ENUM(NSInteger, QMUINavigationButtonPosition) {
QMUINavigationButtonPositionNone = -1, // 不处于navigationBar最左(右)边的按钮,则使用None。用None则不会在alignmentRectInsets里调整位置
QMUINavigationButtonPositionLeft, // 用于leftBarButtonItem,如果用于leftBarButtonItems,则只对最左边的item使用,其他item使用QMUINavigationButtonPositionNone
QMUINavigationButtonPositionRight, // 用于rightBarButtonItem,如果用于rightBarButtonItems,则只对最右边的item使用,其他item使用QMUINavigationButtonPositionNone
};
/**
* 提供以下功能:
* <ol>
* <li>highlighted、disabled状态均通过改变整个按钮的alpha来表现,无需分别设置不同state下的titleColor、image</li>
* <li>支持点击时改变背景色颜色(<i>highlightedBackgroundColor</i>)</li>
* <li>支持点击时改变边框颜色(<i>highlightedBorderColor</i>)</li>
* <li>支持设置图片在按钮内的位置,无需自行调整imageEdgeInsets(<i>imagePosition</i>)</li>
* <li>支持设置按钮内文本和图片之间的间距,无需自行调整titleEdgeInests、imageEdgeInsets、contentEdgeInsets(<i>spacingBetweenImageAndTitle</i>)</li>
* </ol>
*/
@interface QMUIButton : UIButton
/**
* 让按钮的文字颜色自动跟随tintColor调整(系统默认titleColor是不跟随的)<br/>
* 默认为NO
*/
@property(nonatomic, assign) IBInspectable BOOL adjustsTitleTintColorAutomatically;
/**
* 让按钮的图片颜色自动跟随tintColor调整(系统默认image是需要更改renderingMode才可以达到这种效果)<br/>
* 默认为NO
*/
@property(nonatomic, assign) IBInspectable BOOL adjustsImageTintColorAutomatically;
/**
* 是否自动调整highlighted时的按钮样式,默认为YES。<br/>
* 当值为YES时,按钮highlighted时会改变自身的alpha属性为<b>ButtonHighlightedAlpha</b>
*/
@property(nonatomic, assign) IBInspectable BOOL adjustsButtonWhenHighlighted;
/**
* 是否自动调整disabled时的按钮样式,默认为YES。<br/>
* 当值为YES时,按钮disabled时会改变自身的alpha属性为<b>ButtonDisabledAlpha</b>
*/
@property(nonatomic, assign) IBInspectable BOOL adjustsButtonWhenDisabled;
/**
* 设置按钮点击时的背景色,默认为nil。
* @warning 不支持带透明度的背景颜色。当设置<i>highlightedBackgroundColor</i>时,会强制把<i>adjustsButtonWhenHighlighted</i>设为NO,避免两者效果冲突。
* @see adjustsButtonWhenHighlighted
*/
@property(nonatomic, strong) IBInspectable UIColor *highlightedBackgroundColor;
/**
* 设置按钮点击时的边框颜色,默认为nil。
* @warning 当设置<i>highlightedBorderColor</i>时,会强制把<i>adjustsButtonWhenHighlighted</i>设为NO,避免两者效果冲突。
* @see adjustsButtonWhenHighlighted
*/
@property(nonatomic, strong) IBInspectable UIColor *highlightedBorderColor;
/**
* 设置按钮里图标和文字的相对位置,默认为QMUIButtonImagePositionLeft<br/>
* 可配合imageEdgeInsets、titleEdgeInsets、contentHorizontalAlignment、contentVerticalAlignment使用
*/
@property(nonatomic, assign) QMUIButtonImagePosition imagePosition;
/**
* 设置按钮里图标和文字之间的间隔,会自动响应 imagePosition 的变化而变化,默认为0。<br/>
* 系统默认实现需要同时设置 titleEdgeInsets 和 imageEdgeInsets,同时还需考虑 contentEdgeInsets 的增加(否则不会影响布局,可能会让图标或文字溢出或挤压),使用该属性可以避免以上情况。<br/>
* @warning 会与 imageEdgeInsets、 imageEdgeInsets、 contentEdgeInsets 共同作用。
*/
@property(nonatomic, assign) CGFloat spacingBetweenImageAndTitle;
@end
/**
* QMUINavigationButton 是用于 UINavigationItem 的按钮,有两种使用方式:
* 1. 利用类方法,快速生成所需的 UIBarButtonItem,其中大部分 UIBarButtonItem 均使用系统的 initWithBarButtonSystemItem 或 initWithImage 接口创建,仅有返回按钮利用了 customView 来创建 UIBarButtonItem。
* 2. 利用 init 方法生成一个 QMUINavigationButton 实例,再通过类方法 + barButtonItemWithNavigationButton:position:target:action: 来生成一个对应的 UIBarButtonItem,此时 QMUINavigationButton 将作为 UIBarButtonItem 的 customView。
* 若能满足需求,建议优先使用第 1 种方式。
* @note 关于 tintColor:UIBarButtonItem 如果使用了 customView,则需要修改 customView.tintColor,如果没使用 customView,则直接修改 UIBarButtonItem.tintColor。
*/
@interface QMUINavigationButton : UIButton
/**
* 获取当前按钮的`QMUINavigationButtonType`
*/
@property(nonatomic, assign, readonly) QMUINavigationButtonType type;
/**
* 设置按钮是否用于UINavigationBar上的UIBarButtonItem。若为YES,则会参照系统的按钮布局去更改QMUINavigationButton的内容布局,若为NO,则内容布局与普通按钮没差别。默认为YES。
*/
@property(nonatomic, assign) BOOL useForBarButtonItem;
/**
* 导航栏按钮的初始化函数,指定的初始化方法
* @param type 按钮类型
* @param title 按钮的title
*/
- (instancetype)initWithType:(QMUINavigationButtonType)type title:(NSString *)title;
/**
* 导航栏按钮的初始化函数
* @param type 按钮类型
*/
- (instancetype)initWithType:(QMUINavigationButtonType)type;
/**
* 导航栏按钮的初始化函数
* @param image 按钮的image
*/
- (instancetype)initWithImage:(UIImage *)image;
/**
* 创建一个 type 为 QMUINavigationButtonTypeBack 的 button 并作为 customView 用于生成一个 UIBarButtonItem,返回按钮的图片由配置表里的宏 NavBarBackIndicatorImage 决定。
* @param target 按钮点击事件的接收者
* @param selector 按钮点击事件的方法
* @param tintColor 按钮要显示的颜色,如果为 nil,则表示跟随当前 UINavigationBar 的 tintColor
*/
+ (UIBarButtonItem *)backBarButtonItemWithTarget:(id)target action:(SEL)selector tintColor:(UIColor *)tintColor;
/**
* 创建一个 type 为 QMUINavigationButtonTypeBack 的 button 并作为 customView 用于生成一个 UIBarButtonItem,返回按钮的图片由配置表里的宏 NavBarBackIndicatorImage 决定,按钮颜色跟随 UINavigationBar 的 tintColor。
* @param target 按钮点击事件的接收者
* @param selector 按钮点击事件的方法
*/
+ (UIBarButtonItem *)backBarButtonItemWithTarget:(id)target action:(SEL)selector;
/**
* 创建一个以 “×” 为图标的关闭按钮,图片由配置表里的宏 NavBarCloseButtonImage 决定。
* @param target 按钮点击事件的接收者
* @param selector 按钮点击事件的方法
* @param tintColor 按钮要显示的颜色,如果为 nil,则表示跟随当前 UINavigationBar 的 tintColor
*/
+ (UIBarButtonItem *)closeBarButtonItemWithTarget:(id)target action:(SEL)selector tintColor:(UIColor *)tintColor;
/**
* 创建一个以 “×” 为图标的关闭按钮,图片由配置表里的宏 NavBarCloseButtonImage 决定,图片颜色跟随 UINavigationBar.tintColor。
* @param target 按钮点击事件的接收者
* @param selector 按钮点击事件的方法
*/
+ (UIBarButtonItem *)closeBarButtonItemWithTarget:(id)target action:(SEL)selector;
/**
* 创建一个 UIBarButtonItem
* @param type 按钮的类型
* @param title 按钮的标题
* @param tintColor 按钮的颜色,如果为 nil,则表示跟随当前 UINavigationBar 的 tintColor
* @param position 按钮在 UINavigationBar 上的左右位置,如果某一边的按钮有多个,则只有最左边(最右边)的按钮需要设置为 QMUINavigationButtonPositionLeft(QMUINavigationButtonPositionRight),靠里的按钮使用 QMUINavigationButtonPositionNone 即可
* @param target 按钮点击事件的接收者
* @param selector 按钮点击事件的方法
*/
+ (UIBarButtonItem *)barButtonItemWithType:(QMUINavigationButtonType)type title:(NSString *)title tintColor:(UIColor *)tintColor position:(QMUINavigationButtonPosition)position target:(id)target action:(SEL)selector;
/**
* 创建一个 UIBarButtonItem
* @param type 按钮的类型
* @param title 按钮的标题
* @param position 按钮在 UINavigationBar 上的左右位置,如果某一边的按钮有多个,则只有最左边(最右边)的按钮需要设置为 QMUINavigationButtonPositionLeft(QMUINavigationButtonPositionRight),靠里的按钮使用 QMUINavigationButtonPositionNone 即可
* @param target 按钮点击事件的接收者
* @param selector 按钮点击事件的方法
*/
+ (UIBarButtonItem *)barButtonItemWithType:(QMUINavigationButtonType)type title:(NSString *)title position:(QMUINavigationButtonPosition)position target:(id)target action:(SEL)selector;
/**
* 将参数传进来的 button 作为 customView 用于生成一个 UIBarButtonItem。
* @param button 要作为 customView 的 QMUINavigationButton
* @param tintColor 按钮的颜色,如果为 nil,则表示跟随当前 UINavigationBar 的 tintColor
* @param position 按钮在 UINavigationBar 上的左右位置,如果某一边的按钮有多个,则只有最左边(最右边)的按钮需要设置为 QMUINavigationButtonPositionLeft(QMUINavigationButtonPositionRight),靠里的按钮使用 QMUINavigationButtonPositionNone 即可
* @param target 按钮点击事件的接收者
* @param selector 按钮点击事件的方法
*
* @note tintColor、position、target、selector 等参数不需要对 QMUINavigationButton 设置,通过参数传进来就可以了,就算设置了也会在这个方法里被覆盖。
*/
+ (UIBarButtonItem *)barButtonItemWithNavigationButton:(QMUINavigationButton *)button tintColor:(UIColor *)tintColor position:(QMUINavigationButtonPosition)position target:(id)target action:(SEL)selector;
/**
* 将参数传进来的 button 作为 customView 用于生成一个 UIBarButtonItem。
* @param button 要作为 customView 的 QMUINavigationButton
* @param position 按钮在 UINavigationBar 上的左右位置,如果某一边的按钮有多个,则只有最左边(最右边)的按钮需要设置为 QMUINavigationButtonPositionLeft(QMUINavigationButtonPositionRight),靠里的按钮使用 QMUINavigationButtonPositionNone 即可
* @param target 按钮点击事件的接收者
* @param selector 按钮点击事件的方法
*
* @note position、target、selector 等参数不需要对 QMUINavigationButton 设置,通过参数传进来就可以了,就算设置了也会在这个方法里被覆盖。
*/
+ (UIBarButtonItem *)barButtonItemWithNavigationButton:(QMUINavigationButton *)button position:(QMUINavigationButtonPosition)position target:(id)target action:(SEL)selector;
/**
* 创建一个图片类型的 UIBarButtonItem
* @param image 按钮的图标
* @param tintColor 按钮的颜色,如果为 nil,则表示跟随当前 UINavigationBar 的 tintColor
* @param position 按钮在 UINavigationBar 上的左右位置,如果某一边的按钮有多个,则只有最左边(最右边)的按钮需要设置为 QMUINavigationButtonPositionLeft(QMUINavigationButtonPositionRight),靠里的按钮使用 QMUINavigationButtonPositionNone 即可
* @param target 按钮点击事件的接收者
* @param selector 按钮点击事件的方法
*/
+ (UIBarButtonItem *)barButtonItemWithImage:(UIImage *)image tintColor:(UIColor *)tintColor position:(QMUINavigationButtonPosition)position target:(id)target action:(SEL)selector;
/**
* 创建一个图片类型的 UIBarButtonItem
* @param image 按钮的图标
* @param position 按钮在 UINavigationBar 上的左右位置,如果某一边的按钮有多个,则只有最左边(最右边)的按钮需要设置为 QMUINavigationButtonPositionLeft(QMUINavigationButtonPositionRight),靠里的按钮使用 QMUINavigationButtonPositionNone 即可
* @param target 按钮点击事件的接收者
* @param selector 按钮点击事件的方法
*/
+ (UIBarButtonItem *)barButtonItemWithImage:(UIImage *)image position:(QMUINavigationButtonPosition)position target:(id)target action:(SEL)selector;
@end
/**
* `QMUIToolbarButton`是用于底部工具栏的按钮
*/
@interface QMUIToolbarButton : UIButton
/// 获取当前按钮的type
@property(nonatomic, assign, readonly) QMUIToolbarButtonType type;
/**
* 工具栏按钮的初始化函数
* @param type 按钮类型
*/
- (instancetype)initWithType:(QMUIToolbarButtonType)type;
/**
* 工具栏按钮的初始化函数
* @param type 按钮类型
* @param title 按钮的title
*/
- (instancetype)initWithType:(QMUIToolbarButtonType)type title:(NSString *)title;
/**
* 工具栏按钮的初始化函数
* @param image 按钮的image
*/
- (instancetype)initWithImage:(UIImage *)image;
/// 在原有的QMUIToolbarButton上创建一个UIBarButtonItem
+ (UIBarButtonItem *)barButtonItemWithToolbarButton:(QMUIToolbarButton *)button target:(id)target action:(SEL)selector;
/// 创建一个特定type的UIBarButtonItem
+ (UIBarButtonItem *)barButtonItemWithType:(QMUIToolbarButtonType)type title:(NSString *)title target:(id)target action:(SEL)selector;
/// 创建一个图标类型的UIBarButtonItem
+ (UIBarButtonItem *)barButtonItemWithImage:(UIImage *)image target:(id)target action:(SEL)selector;
@end
/**
* 支持显示下划线的按钮,可用于需要链接的场景。下划线默认和按钮宽度一样,可通过 `underlineInsets` 调整。
*/
@interface QMUILinkButton : QMUIButton
/// 控制下划线隐藏或显示,默认为NO,也即显示下划线
@property(nonatomic, assign) IBInspectable BOOL underlineHidden;
/// 设置下划线的宽度,默认为 1
@property(nonatomic, assign) IBInspectable CGFloat underlineWidth;
/// 控制下划线颜色,若设置为nil,则使用当前按钮的titleColor的颜色作为下划线的颜色。默认为 nil。
@property(nonatomic, strong) IBInspectable UIColor *underlineColor;
/// 下划线的位置是基于 titleLabel 的位置来计算的,默认x、width均和titleLabel一致,而可以通过这个属性来调整下划线的偏移值。默认为UIEdgeInsetsZero。
@property(nonatomic, assign) UIEdgeInsets underlineInsets;
@end
/**
* 用于 `QMUIGhostButton.cornerRadius` 属性,当 `cornerRadius` 为 `QMUIGhostButtonCornerRadiusAdjustsBounds` 时,`QMUIGhostButton` 会在高度变化时自动调整 `cornerRadius`,使其始终保持为高度的 1/2。
*/
extern const CGFloat QMUIGhostButtonCornerRadiusAdjustsBounds;
/**
* “幽灵”按钮,也即背景透明、带圆角边框的按钮
*
* 可通过 `QMUIGhostButtonColor` 设置几种预设的颜色,也可以用 `ghostColor` 设置自定义颜色。
*
* @warning 默认情况下,`ghostColor` 只会修改文字和边框的颜色,如果需要让 image 也跟随 `ghostColor` 的颜色,则可将 `adjustsImageWithGhostColor` 设为 `YES`
*/
@interface QMUIGhostButton : QMUIButton
@property(nonatomic, strong) IBInspectable UIColor *ghostColor; // 默认为 GhostButtonColorBlue
@property(nonatomic, assign) CGFloat borderWidth UI_APPEARANCE_SELECTOR; // 默认为 1pt
@property(nonatomic, assign) CGFloat cornerRadius UI_APPEARANCE_SELECTOR; // 默认为 QMUIGhostButtonCornerRadiusAdjustsBounds,也即固定保持按钮高度的一半。
/**
* 控制按钮里面的图片是否也要跟随 `ghostColor` 一起变化,默认为 `NO`
*/
@property(nonatomic, assign) BOOL adjustsImageWithGhostColor UI_APPEARANCE_SELECTOR;
- (instancetype)initWithGhostType:(QMUIGhostButtonColor)ghostType;
- (instancetype)initWithGhostColor:(UIColor *)ghostColor;
@end
/**
* 用于 `QMUIFillButton.cornerRadius` 属性,当 `cornerRadius` 为 `QMUIFillButtonCornerRadiusAdjustsBounds` 时,`QMUIFillButton` 会在高度变化时自动调整 `cornerRadius`,使其始终保持为高度的 1/2。
*/
extern const CGFloat QMUIFillButtonCornerRadiusAdjustsBounds;
/**
* QMUIFillButton
* 实心填充颜色的按钮,支持预定义的几个色值
*/
@interface QMUIFillButton : QMUIButton
@property(nonatomic, strong) IBInspectable UIColor *fillColor; // 默认为 FillButtonColorBlue
@property(nonatomic, strong) IBInspectable UIColor *titleTextColor; // 默认为 UIColorWhite
@property(nonatomic, assign) CGFloat cornerRadius UI_APPEARANCE_SELECTOR;// 默认为 QMUIFillButtonCornerRadiusAdjustsBounds,也即固定保持按钮高度的一半。
/**
* 控制按钮里面的图片是否也要跟随 `titleTextColor` 一起变化,默认为 `NO`
*/
@property(nonatomic, assign) BOOL adjustsImageWithTitleTextColor UI_APPEARANCE_SELECTOR;
- (instancetype)initWithFillType:(QMUIFillButtonColor)fillType;
- (instancetype)initWithFillType:(QMUIFillButtonColor)fillType frame:(CGRect)frame;
- (instancetype)initWithFillColor:(UIColor *)fillColor titleTextColor:(UIColor *)textColor;
- (instancetype)initWithFillColor:(UIColor *)fillColor titleTextColor:(UIColor *)textColor frame:(CGRect)frame;
@end