IOS 修改UIAlertController的按钮标题的字体颜色,字号,内容

时间:2025-01-18 22:54:53
- (IBAction)buttonClick:(UIButton *)sender { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleAlert]; //修改标题的内容,字号,颜色。使用的key值是“attributedTitle” NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"heihei"]; [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50] range:NSMakeRange(0, [[hogan string] length])]; [hogan addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [[hogan string] length])]; [alertController setValue:hogan forKey:@"attributedTitle"]; //修改按钮的颜色,同上可以使用同样的方法修改内容,样式 UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:nil]; [defaultAction setValue:[UIColor blueColor] forKey:@"_titleTextColor"]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; [cancelAction setValue:[UIColor greenColor] forKey:@"_titleTextColor"]; [alertController addAction:defaultAction]; [alertController addAction:cancelAction]; [self presentViewController:alertController animated:YES completion:nil]; }