iOS開發 自定義UIAlertController的樣式


引言:

關於提示框, 系統自帶的提示框有時可能滿足不了我們的需求, 比如一個提示框的取消按鈕我需要灰色字體顯示, 這時候就需要自定義提示框的樣式了。


示例圖

蘋果自iOS8開始,就已經廢棄了之前用於界面提醒的UIAlertView類以及UIActionSheet,取而代之的是UIAlertController以及UIAlertAction,從實際使用情況來看,蘋果把之前不同類型/樣式的通知實現方法進行了統一,簡化了有關提醒功能的實現。

UIAlertController的基本使用

一個簡單的提示框:

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"標題" message:@"正文" preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { // 點擊確定按鈕時 要進行的操作可以寫到這里 }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) { // 點擊取消按鈕時 要進行的操作可以寫到這里 }]; [alert addAction:cancelAction]; [alert addAction:okAction]; [self presentViewController:alert animated:YES completion:nil];

自定義UIAlertController

主要是使用kvc的方式來自定義UIAlertController的樣式:

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"標題" message:@"內容" preferredStyle:UIAlertControllerStyleAlert]; // 使用富文本來改變alert的title字體大小和顏色 NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:@"這里是標題"]; [titleText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24] range:NSMakeRange(0, 2)]; [titleText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)]; [alert setValue:titleText forKey:@"attributedTitle"]; // 使用富文本來改變alert的message字體大小和顏色 // NSMakeRange(0, 2) 代表:從0位置開始 兩個字符 NSMutableAttributedString *messageText = [[NSMutableAttributedString alloc] initWithString:@"這里是正文信息"]; [messageText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, 6)]; [messageText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)]; [messageText addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:NSMakeRange(3, 3)]; [alert setValue:messageText forKey:@"attributedMessage"]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; // 設置按鈕背景圖片 UIImage *accessoryImage = [[UIImage imageNamed:@"selectRDImag.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [cancelAction setValue:accessoryImage forKey:@"image"]; // 設置按鈕的title顏色 [cancelAction setValue:[UIColor lightGrayColor] forKey:@"titleTextColor"]; // 設置按鈕的title的對齊方式 [cancelAction setValue:[NSNumber numberWithInteger:NSTextAlignmentLeft] forKey:@"titleTextAlignment"]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:okAction]; [alert addAction:cancelAction]; [self presentViewController:alert animated:YES completion:nil];

效果圖:


效果圖

demo下載地址:CustomAlertControllerDemo





原文鏈接:http://www.jianshu.com/p/a0785cb0601b


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM