UIAlertView
一般使用提示框,會(huì)使用“UIAlertView”!
首先遵守<UIAlertViewDelegate>
協(xié)議!
UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"標(biāo)題" message:@"真的要退出此賬號(hào)么?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alertV show]; //展示提示框
效果:
使用“UIAlertViewDelegate”,響應(yīng) 按鈕點(diǎn)擊事件!
#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { //按鈕點(diǎn)擊
if (buttonIndex == 1) { //確認(rèn)按鈕
//確認(rèn)的操作
}
}
可通過(guò)更改UIAlertView的
alertViewStyle
屬性來(lái)實(shí)現(xiàn)文字輸入的效果。UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"發(fā)到郵箱" message:@"請(qǐng)輸入郵箱地址" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確認(rèn)", nil]; //設(shè)置 含有文字輸入框 [alertV setAlertViewStyle:UIAlertViewStylePlainTextInput]; [alertV show]; //獲取文字輸入框(UITextField *) UITextField * textStrTF = [alertV textFieldAtIndex:0]; textStrTF.keyboardType = UIKeyboardTypeEmailAddress; //郵箱鍵盤(pán) textStrTF.placeholder = @"請(qǐng)輸入郵箱地址"; textStrTF.text = [User_Def objectForKey:USER_EMail]?[User_Def objectForKey:USER_EMail]:@""; textStrTF.clearButtonMode = UITextFieldViewModeWhileEditing; //編輯時(shí)有刪除按鈕
效果:
UIAlertController
改變字體(富文本)的顏色、大小時(shí),最好使用UIAlertController!
(KVC方式 [setValue: forKey:]
)
-
改變 按鈕字體顏色
NSString * titleStr = NSLocalizedString(@"標(biāo)題", nil); //標(biāo)題 UIAlertController * alertcontroller = [UIAlertController alertControllerWithTitle: titleStr message:@"真的要退出此賬號(hào)么?" preferredStyle:UIAlertControllerStyleAlert]; //確認(rèn)按鈕 UIAlertAction *sureAction=[UIAlertAction actionWithTitle:NSLocalizedString(@"確定", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //確認(rèn)的操作 }]; 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項(xiàng) 添加到UIAlertController [alertcontroller addAction:cancelAction]; [alertcontroller addAction: sureAction]; //present 顯示alertcontroller [self presentViewController:alertcontroller animated:YES completion:nil];
效果:
-
改變標(biāo)題、及提示信息的字體:(富文本)
//改變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 = @"真的要退出此賬號(hào)么?"; //改變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 = @"輸入文字"; }];
表格形式
當(dāng)
preferredStyle
設(shè)置為UIAlertControllerStyleActionSheet
!(表格形式)
使用KVC方式 ([setValue: forKey:]
),改變字體的大小和顏色!!