1. 程式人生 > >iOS UIAlertController上修改標題(title) 訊息(message) 按鈕 的字型大小及顏色

iOS UIAlertController上修改標題(title) 訊息(message) 按鈕 的字型大小及顏色

Demo GitHub地址:點選開啟連結

之前的程式做了一個可以輸入的訊息彈出框,樣式如下:


那麼問題就來了,我現在想修改一下title、message、按鈕的字型大小和顏色,在網上查閱了一些資料,完成了修改。


<span style="font-size:24px;">oc程式碼如下:</span><span style="font-size:24px; font-family: Arial, Helvetica, sans-serif;">使用</span><span class="s1" style="font-size:24px; font-family: Arial, Helvetica, sans-serif;">KVC的方式</span>
<span style="font-size:18px;"> UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"請修改輸入內容" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"內容";
//        [textField setFont:[UIFont systemFontOfSize:16]];
    }];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        UITextField *login = alertController.textFields.firstObject;
        
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
    }];
    /*title*/
    NSMutableAttributedString *alertTitleStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];
    [alertTitleStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 2)];
    [alertTitleStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [alertController setValue:alertTitleStr forKey:@"attributedTitle"];
    
    /*message*/
    NSMutableAttributedString *alertMessageStr = [[NSMutableAttributedString alloc] initWithString:@"請修改輸入內容"];
    [alertMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, 7)];
    [alertMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
    [alertMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3, 2)];
    [alertMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 2)];
    [alertController setValue:alertMessageStr forKey:@"attributedMessage"];
    
    /*取消按鈕的顏色*/
    [cancel setValue:[UIColor redColor] forKey:@"_titleTextColor"];
    [alertController addAction:cancel];
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];</span>

參考了文章:http://stackoverflow.com/questions/26460706/uialertcontroller-custom-font-size-color