UIAlertView
一般使用提示框,会使用“UIAlertView”!
首先遵守<UIAlertViewDelegate>
协议!
UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"标题" message:@"真的要退出此账号么?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertV show]; //展示提示框
效果:
使用“UIAlertViewDelegate”,响应 按钮点击事件!
#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { //按钮点击
if (buttonIndex == 1) { //确认按钮
//确认的操作
}
}
可通过更改UIAlertView的
alertViewStyle
属性来实现文字输入的效果。UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"发到邮箱" message:@"请输入邮箱地址" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil]; //设置 含有文字输入框 [alertV setAlertViewStyle:UIAlertViewStylePlainTextInput]; [alertV show]; //获取文字输入框(UITextField *) UITextField * textStrTF = [alertV textFieldAtIndex:0]; textStrTF.keyboardType = UIKeyboardTypeEmailAddress; //邮箱键盘 textStrTF.placeholder = @"请输入邮箱地址"; textStrTF.text = [User_Def objectForKey:USER_EMail]?[User_Def objectForKey:USER_EMail]:@""; textStrTF.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时有删除按钮
效果:
UIAlertController
改变字体(富文本)的颜色、大小时,最好使用UIAlertController!
(KVC方式 [setValue: forKey:]
)
-
改变 按钮字体颜色
NSString * titleStr = NSLocalizedString(@"标题", nil); //标题 UIAlertController * alertcontroller = [UIAlertController alertControllerWithTitle: titleStr message:@"真的要退出此账号么?" preferredStyle:UIAlertControllerStyleAlert]; //确认按钮 UIAlertAction *sureAction=[UIAlertAction actionWithTitle:NSLocalizedString(@"确定", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //确认的操作 }]; UIColor * changeColor = [UIColor redColor]; //颜色 [sureAction setValue: changeColor forKey:@"titleTextColor"]; //KVC:改变字体颜色 //取消按钮 UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:NSLocalizedString(@"取消", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //取消的操作 }]; //[cancelAction setValue:[UIColor greenColor] forKey:@"titleTextColor"]; //KVC:改变字体颜色 //将UIAlertAction项 添加到UIAlertController [alertcontroller addAction:cancelAction]; [alertcontroller addAction: sureAction]; //present 显示alertcontroller [self presentViewController:alertcontroller animated:YES completion:nil];
效果:
-
改变标题、及提示信息的字体:(富文本)
//改变title的大小和颜色 NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:titleStr]; [titleAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16.f] range:NSMakeRange(0, titleStr.length)]; [titleAtt addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, titleStr.length)]; //KVC:改变字体(富文本) [alertcontroller setValue:titleAtt forKey:@"attributedTitle"]; NSString * messageStr = @"真的要退出此账号么?"; //改变message的大小和颜色 NSMutableAttributedString *messageAtt = [[NSMutableAttributedString alloc] initWithString:messageStr]; [messageAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13.f] range:NSMakeRange(0, messageStr.length)]; [messageAtt addAttribute:NSForegroundColorAttributeName value:[UIColor cyanColor] range:NSMakeRange(0, messageStr.length)]; //KVC:改变字体(富文本) [alertcontroller setValue:messageAtt forKey:@"attributedMessage"];
效果:
UIAlertController添加 文字输入框:
[alertcontroller addTextFieldWithConfigurationHandler:^(UITextField *textField){ textField.placeholder = @"输入文字"; }];
表格形式
当
preferredStyle
设置为UIAlertControllerStyleActionSheet
!(表格形式)
使用KVC方式 ([setValue: forKey:]
),改变字体的大小和颜色!!