之前的程序做了一个可以输入的消息弹出框,样式如下:
那么问题就来了,我现在想修改一下title、message、按钮的字体大小和颜色,在网上查阅了一些资料,完成了修改。
使用KVC的方式:
UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"请修改输入内容"preferredStyle:UIAlertControllerStyleAlert];
[alertControlleraddTextFieldWithConfigurationHandler:^(UITextField*textField){
textField.placeholder=@"内容";
// [textField setFont:[UIFont systemFontOfSize:16]];
}];
UIAlertAction*okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
UITextField*login = alertController.textFields.firstObject;
}];
UIAlertAction*cancel = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
}];
/*title*/
NSMutableAttributedString*alertTitleStr = [[NSMutableAttributedStringalloc]initWithString:@"提示"];
[alertTitleStraddAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:20]range:NSMakeRange(0,2)];
[alertTitleStraddAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(0,2)];
[alertControllersetValue:alertTitleStrforKey:@"attributedTitle"];
/*message*/
NSMutableAttributedString*alertMessageStr = [[NSMutableAttributedStringalloc]initWithString:@"请修改输入内容"];
[alertMessageStraddAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:18]range:NSMakeRange(0,7)];
[alertMessageStraddAttribute:NSForegroundColorAttributeNamevalue:[UIColororangeColor]range:NSMakeRange(0,3)];
[alertMessageStraddAttribute:NSForegroundColorAttributeNamevalue:[UIColorgreenColor]range:NSMakeRange(3,2)];
[alertMessageStraddAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:NSMakeRange(5,2)];
[alertControllersetValue:alertMessageStrforKey:@"attributedMessage"];
/*取消按钮的颜色*/
[cancelsetValue:[UIColorredColor]forKey:@"_titleTextColor"];
[alertControlleraddAction:cancel];
[alertControlleraddAction:okAction];
[selfpresentViewController:alertControlleranimated:YEScompletion:nil];