The use of UIAlertController and the modification of font color size

Pop-up windows can be said to be a very common skill in our daily development, and there are many ways to pop-up windows. UIAlertView was the most used before, but after iOS8, UIAlertController may be used more. The more you use, the more requirements. Maybe sometimes the default font size and text color of the system cannot meet our needs. At this time, we are required to modify the color and size of the text. Next, I will introduce the use of KVC to modify the font color of the title and content of UIAlertController

normal popup code

   // 初始化UIAlertController
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"此处展示提示消息" preferredStyle:UIAlertControllerStyleActionSheet];
    // 添加UIAlertAction
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确认");
    }];
    [alertController addAction:sureAction];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    [alertController addAction:cancelAction];

    UIAlertAction *testActionOne = [UIAlertAction actionWithTitle:@"测试1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"测试1");
    }];
    [alertController addAction:testActionOne];

    UIAlertAction *testActionTwo = [UIAlertAction actionWithTitle:@"测试2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"测试2");
    }];
    [alertController addAction:testActionTwo];
     // 弹窗提示窗口
    [self presentViewController:alertController animated:YES completion:nil];

The pop-up window display effect is as follows:
write picture description here

Modify popup font color

The following is the complete method of modifying the text color size of the title prompt information and modifying the font color of the content. Students who want to be lazy can directly copy the code below to the position where they want to pop up the window, and then modify the corresponding value according to their own needs.

    // 初始化UIAlertController
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

    //修改title字体及颜色
    NSMutableAttributedString *titleStr = [[NSMutableAttributedString alloc] initWithString:@"标题"];
    [titleStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, titleStr.length)];
    [titleStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, titleStr.length)];
    [alertController setValue:titleStr forKey:@"attributedTitle"];

    // 修改message字体及颜色
    NSMutableAttributedString *messageStr = [[NSMutableAttributedString alloc] initWithString:@"此处展示提示消息"];
    [messageStr addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(0, messageStr.length)];
    [messageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, messageStr.length)];
    [alertController setValue:messageStr forKey:@"attributedMessage"];


    // 添加UIAlertAction
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确认");
    }];
    // KVC修改字体颜色
    [sureAction setValue:[UIColor blueColor] forKey:@"_titleTextColor"];
    [alertController addAction:sureAction];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    [cancelAction setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
    [alertController addAction:cancelAction];

    UIAlertAction *testActionOne = [UIAlertAction actionWithTitle:@"测试1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"测试1");
    }];
    [testActionOne setValue:[UIColor greenColor] forKey:@"_titleTextColor"];
    [alertController addAction:testActionOne];

    UIAlertAction *testActionTwo = [UIAlertAction actionWithTitle:@"测试2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"测试2");
    }];
    [testActionTwo setValue:[UIColor greenColor] forKey:@"_titleTextColor"];
    [alertController addAction:testActionTwo];

    // 弹窗提示窗口
    [self presentViewController:alertController animated:YES completion:nil];

The effect diagram after the modification of the above code is shown as follows:
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325978582&siteId=291194637