Skip to content

Commit 517ea8b

Browse files
1.不重写desriptionTextColor的setter,因为appearance属性不是在调用set的时候马上就调用了次方法
2.title和description支持NSString和NSAttributedString
1 parent e381ab2 commit 517ea8b

3 files changed

Lines changed: 42 additions & 17 deletions

File tree

FEAlertController/FEAlertContentView.m

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ -(void)setCornerRadius:(CGFloat)cornerRadius{
3333
self.layer.cornerRadius = cornerRadius;
3434
}
3535

36-
-(void)setDescriptionTextColor:(UIColor *)descriptionTextColor{
37-
_descriptionTextColor = descriptionTextColor;
38-
39-
self.descriptionLabel.textColor = descriptionTextColor;
40-
}
41-
4236
#pragma mark -
4337

4438
-(void)highlightButtonByIndex:(NSInteger)buttonIndex{

FEAlertController/FEAlertController.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ typedef void(^FEAlertControllerCallback)(FEAlertController *alertController, NSI
1717

1818
@property (nonatomic, strong) FEAlertContentView *contentView;
1919

20-
@property (nonatomic, copy) NSString *alertTitle;
20+
@property (nonatomic, copy) id alertTitle; // NSString or NSAttributedString
2121
@property (nonatomic, strong) UIImage *alertImage;
2222
@property (nonatomic, strong) NSArray *alertAnimationImages; // for animation
23-
@property (nonatomic, copy) NSString *alertDescription;
23+
@property (nonatomic, copy) id alertDescription; // NSString or NSAttributedString
2424
@property (nonatomic, strong) NSArray *alertButtons;
2525
@property (nonatomic, assign) NSInteger highlightButtonIndex;
2626

@@ -40,9 +40,9 @@ typedef void(^FEAlertControllerCallback)(FEAlertController *alertController, NSI
4040
*
4141
* @return a alert ready to present
4242
*/
43-
+(instancetype)alertWithTitle:(NSString *)title
43+
+(instancetype)alertWithTitle:(id)title
4444
image:(UIImage *)image
45-
description:(NSString *)description
45+
description:(id)description
4646
buttons:(NSArray *)buttons
4747
highlightButtonIndex:(NSInteger)highlightButtonIndex
4848
callback:(FEAlertControllerCallback)callback;
@@ -58,9 +58,9 @@ typedef void(^FEAlertControllerCallback)(FEAlertController *alertController, NSI
5858
*
5959
* @return a alert ready to present
6060
*/
61-
+ (instancetype)showWithTitle:(NSString *)title
61+
+ (instancetype)showWithTitle:(id)title
6262
image:(UIImage *)image
63-
description:(NSString *)description
63+
description:(id)description
6464
buttons:(NSArray *)buttons
6565
highlightButtonIndex:(NSInteger)highlightButtonIndex
6666
callback:(FEAlertControllerCallback)callback;

FEAlertController/FEAlertController.m

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@
1111

1212
#define FEAlertiOS8Later (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_8_0)
1313

14+
static inline BOOL FE_checkTextValid(id text) {
15+
if ([text isKindOfClass:[NSString class]] || [text isKindOfClass:[NSAttributedString class]]) {
16+
return YES;
17+
}
18+
return NO;
19+
}
20+
21+
static inline BOOL FE_isAttributedString(id text) {
22+
if ([text isKindOfClass:[NSAttributedString class]]) {
23+
return YES;
24+
}
25+
return NO;
26+
}
27+
1428
@interface FEAlertController ()<UIGestureRecognizerDelegate,FEAlertContentViewDelegate>
1529

1630
@property (nonatomic, copy) FEAlertControllerCallback callback;
@@ -129,8 +143,26 @@ -(FEAlertContentView *)contentView{
129143
if (!_contentView) {
130144
_contentView = [FEAlertContentView instanceWithXIB];
131145
_contentView.delegate = self;
132-
_contentView.titleLabel.text = self.alertTitle;
133-
_contentView.descriptionLabel.text = self.alertDescription;
146+
147+
if (FE_isAttributedString(self.alertTitle)) {
148+
_contentView.titleLabel.attributedText = self.alertTitle;
149+
}
150+
else {
151+
_contentView.titleLabel.text = self.alertTitle;
152+
}
153+
154+
if (FE_isAttributedString(self.alertDescription)) {
155+
_contentView.descriptionLabel.attributedText = self.alertDescription;
156+
}
157+
else {
158+
// important
159+
if (!_contentView.descriptionLabel.textColor) {
160+
_contentView.descriptionLabel.textColor = [[FEAlertContentView appearance] descriptionTextColor];
161+
}
162+
163+
_contentView.descriptionLabel.text = self.alertDescription;
164+
}
165+
134166
_contentView.clipsToBounds = YES;
135167

136168
// image or images
@@ -156,9 +188,9 @@ -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveT
156188

157189
+(instancetype)alertWithTitle:(NSString *)title image:(UIImage *)image description:(NSString *)description buttons:(NSArray *)buttons highlightButtonIndex:(NSInteger)highlightButtonIndex callback:(FEAlertControllerCallback)callback{
158190
FEAlertController *alertController = [[FEAlertController alloc] init];
159-
alertController.alertTitle = title;
191+
alertController.alertTitle = FE_checkTextValid(title)?title:nil;
160192
alertController.alertImage = image;
161-
alertController.alertDescription = description;
193+
alertController.alertDescription = FE_checkTextValid(description)?description:nil;
162194
alertController.alertButtons = buttons;
163195

164196
// highlightButtonIndex
@@ -174,7 +206,6 @@ +(instancetype)alertWithTitle:(NSString *)title image:(UIImage *)image descripti
174206
return alertController;
175207
}
176208

177-
178209
- (void)showInNewWindow {
179210
// 记录
180211
self.fromWindow = [UIApplication sharedApplication].keyWindow;

0 commit comments

Comments
 (0)