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

IOS 修改UIAlertController的按钮标题的字体颜色,字号,内容_第1张图片
现在只是实现了功能,原理是通过runtime获取对应的可以修改的key。具体还后续的学习中进行补充

主要的代码:

//按钮点击

- (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];


}

你可能感兴趣的:(iOS学习随笔)