修改原生UIAlertController
的样式
先上个效果图
效果图
实现代码
-(void)alertWithTitle:(NSString*)title andMessage:(NSString*)message{
UIAlertController * alertContorller = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleAlert)];
UIView *subView1 = alertContorller.view.subviews[0];
UIView *subView2 = subView1.subviews[0];
UIView *subView3 = subView2.subviews[0];
UIView *subView4 = subView3.subviews[0];
UIView *subView5 = subView4.subviews[0];
//设置title的对齐方式
UILabel * titleLab = subView5.subviews[0];
titleLab.textAlignment = NSTextAlignmentLeft;
//设置内容的对齐方式
UILabel *mes= subView5.subviews[1];
mes.textAlignment = NSTextAlignmentRight;
//设置 title 的字体颜色 大小
NSDictionary* dic=@{NSForegroundColorAttributeName:[UIColor orangeColor],
NSFontAttributeName:[UIFont systemFontOfSize:28],
NSExpansionAttributeName:@(0.2),
};
NSMutableAttributedString*attributedStr=[[NSMutableAttributedString alloc]initWithString:title attributes:dic];
[alertContorller setValue:attributedStr forKey:@"attributedTitle"];
//设置 message 的字体颜色 大小
NSDictionary* messageDic=@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont systemFontOfSize:12],
NSExpansionAttributeName:@(0.2),
};
NSMutableAttributedString* attributedMesStr=[[NSMutableAttributedString alloc]initWithString:message attributes:messageDic];
[alertContorller setValue:attributedMesStr forKey:@"attributedMessage"];
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"返回" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"返回");
}];
//设置字体颜色
[cancelAction setValue:[UIColor purpleColor] forKey:@"titleTextColor"];
UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"继续选择" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"继续");
}];
//设置字体颜色
[sureAction setValue:[UIColor magentaColor] forKey:@"titleTextColor"];
[alertContorller addAction:cancelAction];
[alertContorller addAction:sureAction];
[self presentViewController:alertContorller animated:YES completion:nil];
}
网友评论