iOS 对系统的提示框进行简单的修改


------    带有输入框的提示框  ------

有时候 , 我们需要带有输入框的提示框, 如图:

iOS 对系统的提示框进行简单的修改_第1张图片

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入个人信息" preferredStyle:UIAlertControllerStyleAlert];

[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

UITextField *userNameTextField = alertController.textFields.firstObject;

UITextField *passwordTextField = alertController.textFields.lastObject;

NSLog(@"用户名 = %@,密码 = %@",userNameTextField.text,passwordTextField.text);

}]];

[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];

[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

textField.placeholder = @"请输入用户名";

}];

[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

textField.placeholder = @"请输入密码";

}];

[self presentViewController:alertController animated:true completion:nil];


------  修改系统提示框的字号和颜色等属性 ------


iOS 对系统的提示框进行简单的修改_第2张图片


UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"提示信息" preferredStyle:UIAlertControllerStyleAlert];

//修改标题的内容,字号,颜色。使用的key值是“attributedTitle”

NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"提示"];

[hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:40] range:NSMakeRange(0, [[hogan string] length])];

[hogan addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [[hogan string] length])];

[alertController setValue:hogan forKey:@"attributedTitle"];

//修改按钮的颜色,同上可以使用同样的方法修改内容,样式

UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:nil];

[defaultAction setValue:[UIColor grayColor] forKey:@"_titleTextColor"];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

[cancelAction setValue:[UIColor greenColor] forKey:@"_titleTextColor"];

[alertController addAction:defaultAction];

[alertController addAction:cancelAction];

[self presentViewController:alertController animated:YES completion:nil];

------  改变提示文字的颜色  ------

这个是系统自带的,  只是改变了提示框的展示样式.

UIAlertActionStyleDestructive


iOS 对系统的提示框进行简单的修改_第3张图片

//显示弹出框列表选择

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title"

message:@"This is an Sheet."

preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消"

style:UIAlertActionStyleCancel

handler:^(UIAlertAction * action) {

//响应事件

NSLog(@"action = %@", action);

}];

UIAlertAction* deleteAction = [UIAlertAction actionWithTitle:@"删除"

style:UIAlertActionStyleDestructive

handler:^(UIAlertAction * action) {

//响应事件

NSLog(@"action = %@", action);

}];

UIAlertAction* saveAction = [UIAlertAction actionWithTitle:@"保存"

style:UIAlertActionStyleDefault

handler:^(UIAlertAction * action) {

//响应事件

NSLog(@"action = %@", action);

}];

[alert addAction:saveAction];

[alert addAction:cancelAction];

[alert addAction:deleteAction];

[self presentViewController:alert animated:YES completion:nil];

你可能感兴趣的:(iOS 对系统的提示框进行简单的修改)