1. 程式人生 > >自定義alert彈框

自定義alert彈框

system div share ons title nbsp tof 接口 har

 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彈框