iOS 自定义UIAlertController的title、message、button的颜色

有时设计要求改弹窗的颜色,so,改吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#import <UIKit/UIKit.h>

@interface UIAlertAction (ZDExtension)

- (void)zd_setTitleColor:(UIColor *)color;

@end

@interface UIAlertController (ZDExtension)

- (void)zd_setTitleColor:(UIColor *)color;

- (void)zd_setAttributedTitle:(NSAttributedString *)title;

- (void)zd_setMessageColor:(UIColor *)color;

- (void)zd_setAttributedMessage:(NSAttributedString *)message;

- (void)zd_setActionTitleColor:(UIColor *)color;

@end
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
@implementation UIAlertAction (ZDExtension)

- (void)zd_setTitleColor:(UIColor *)color {
[self setValue:color forKey:@"titleTextColor"];
}

@end

@implementation UIAlertController (ZDExtension)

- (void)zd_setTitleColor:(UIColor *)color {
NSAttributedString *string = [[NSAttributedString alloc] initWithString:self.title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:color}];
[self zd_setAttributedTitle:string];
}

- (void)zd_setAttributedTitle:(NSAttributedString *)title {
[self setValue:title forKey:@"attributedTitle"];
}

- (void)zd_setMessageColor:(UIColor *)color {
NSAttributedString *string = [[NSAttributedString alloc] initWithString:self.message attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:color}];
[self zd_setAttributedMessage:string];
}

- (void)zd_setAttributedMessage:(NSAttributedString *)message {
[self setValue:message forKey:@"attributedMessage"];
}

- (void)zd_setActionTitleColor:(UIColor *)color {
for (UIAlertAction *action in self.actions) {
[action zd_setTitleColor:color];
}
}

@end

iOS 自定义UIAlertController的title、message、button的颜色
http://example.com/2017/11/28/iOS-自定义UIAlertController的title、message、button的颜色/
作者
guanzhendong
发布于
2017年11月28日
许可协议