自定义alert弹框
Posted 菁欣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义alert弹框相关的知识,希望对你有一定的参考价值。
1 /**************** UIAlertControllerStyleAlert *************************/ 2 /*创建一个 可以自定义文字颜色和字体大小的IAlertView*/ 3 NSString *message = @"开通失败,请再次开通或者联系客服"; 4 NSString *title = @"400-8591-200"; 5 NSString *leftActionStr = @"呼叫"; 6 NSString *rightActionStr = @"继续开通"; 7 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; 8 //改变title的大小和颜色 9 NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:title]; 10 [titleAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:(36/2)*KWidth_Scale] range:NSMakeRange(0, title.length)]; 11 [titleAtt addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x00abea) range:NSMakeRange(0, title.length)]; 12 [alertController setValue:titleAtt forKey:@"attributedTitle"]; 13 //改变message的大小和颜色 14 NSMutableAttributedString *messageAtt = [[NSMutableAttributedString alloc] initWithString:message]; 15 [messageAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:(24/2)*KWidth_Scale] range:NSMakeRange(0, message.length)]; 16 [messageAtt addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x666666) range:NSMakeRange(0, message.length)]; 17 [alertController setValue:messageAtt forKey:@"attributedMessage"]; 18 19 UIAlertAction *alertActionCall = [UIAlertAction actionWithTitle:leftActionStr style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 20 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://400-8591-200"]]; 21 }]; 22 [alertController addAction:alertActionCall]; 23 [alertActionCall setValue:UIColorFromRGB(0x00abea) forKey:@"titleTextColor"]; 24 kWeakSelf(weakSelf); 25 UIAlertAction *alertActionContinue = [UIAlertAction actionWithTitle:rightActionStr style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 26 [weakSelf oprDrugStoreStatu:@"0"]; 27 }]; 28 [alertController addAction:alertActionContinue]; 29 [alertActionContinue setValue:[UIColor blackColor] forKey:@"titleTextColor"]; 30 [self presentViewController:alertController animated:YES completion:nil]; 31 32 33 34 35 /**************** UIAlertControllerStyleActionSheet *************************/ 36 NSString *title = [NSString stringWithFormat:@"将患者\"%@\"删除,同时删除与该联系人的聊天记录", self.user.patientName]; 37 UIAlertController *alertSheet = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 38 39 UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; 40 [cancel setValue:UIColorFromRGB(0x666666) forKey:@"titleTextColor"]; 41 [alertSheet addAction:cancel]; 42 43 UIAlertAction *certain = [UIAlertAction actionWithTitle:@"删除联系人" style:UIAlertActionStyleDestructive handler: 44 ^(UIAlertAction * _Nonnull action) { 45 NSLog(@"执行删除操作,这里应该接着请求删除接口......"); 46 }]; 47 [alertSheet addAction:certain]; 48 [self presentViewController:alertSheet animated:YES completion:nil];
以上是关于自定义alert弹框的主要内容,如果未能解决你的问题,请参考以下文章