iOS8 之后系统把UIActionSheet和UIAlertView合并为了UIAlertController,使用就不比多说了,但在使用的过程中发现样式是固定的,点进去也未发现可以提供更改多样式的属性,所以只能使用KVC在运行时动态访问和修改对象的属性。
但在使用过程中需要知道属性类型对应的key值,这里提供两个方法,是别人写好的,借用一下。
//获得所有变量
- (NSArray *)getAllIvar:(id)object
{
NSMutableArray *array = [NSMutableArray array];
unsigned int count;
Ivar *ivars = class_copyIvarList([object class], &count);
for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i];
const char *keyChar = ivar_getName(ivar);
NSString *keyStr = [NSString stringWithCString:keyChar encoding:NSUTF8StringEncoding];
@try {
id valueStr = [object valueForKey:keyStr];
NSDictionary *dic = nil;
if (valueStr) {
dic = @{keyStr : valueStr};
} else {
dic = @{keyStr : @"值为nil"};
}
[array addObject:dic];
}
@catch (NSException *exception) {}
}
return [array copy];
}
//获得所有属性
- (NSArray *)getAllProperty:(id)object
{
NSMutableArray *array = [NSMutableArray array];
unsigned int count;
objc_property_t *propertys = class_copyPropertyList([object class], &count);
for (int i = 0; i < count; i++) {
objc_property_t property = propertys[i];
const char *nameChar = property_getName(property);
NSString *nameStr = [NSString stringWithCString:nameChar encoding:NSUTF8StringEncoding];
[array addObject:nameStr];
}
return [array copy];
}
获取到属性之后就可以去设置样式了
经常需要设置message左对齐、段间距这些属性
- (IBAction)tapAlertControllerButton:(id)sender {
NSString *titleString = @"商家发货规则";
NSString *messageString = @"1、买家下单付款后,请尽快发货;\n2、买家下单付款后,若超过最迟发货的时间,商家仍未发货,那么后台将自动取消订单;\n3、若遇到物流高峰期,比如春节,双十一等,请点击【延迟发货】来延长最迟发货时间;\n4、每个订单只允许【延迟发货】一次,每次可延迟5天;";
NSString *alertTitle = @"知道了";
UIAlertController *_alertController = [UIAlertController alertControllerWithTitle:titleString message:messageString preferredStyle:UIAlertControllerStyleAlert];
[_alertController addAction:[UIAlertAction actionWithTitle:alertTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
/* 修改title */
NSMutableAttributedString *attTitleString = [[NSMutableAttributedString alloc] initWithString:titleString];
[_alertController setValue:attTitleString forKey:@"attributedTitle"];
/* 修改message */
NSMutableAttributedString *attMsgString = [[NSMutableAttributedString alloc] initWithString:messageString];
// 设置字体
[attMsgString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attMsgString.length)];
// 设置颜色
[attMsgString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 10)];
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
// 设置行间距
[paragraph setLineSpacing:3];
// 设置段间距
[paragraph setParagraphSpacingBefore:5];
// 设置对齐方式
[paragraph setAlignment:NSTextAlignmentLeft];
// 设置书写方向
[paragraph setBaseWritingDirection:NSWritingDirectionLeftToRight];
[attMsgString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, attMsgString.length)];
[_alertController setValue:attMsgString forKey:@"attributedMessage"];
/* 修改按钮的颜色 */
NSArray *actionArr = [_alertController actions];
[actionArr.firstObject setTitle:alertTitle];
[actionArr.firstObject setValue:[UIColor orangeColor] forKey:@"titleTextColor"];
[self presentViewController:_alertController animated:YES completion:nil];
}