iOS UIAlertController上修改标题(title) 消息(message) 按钮 的字体大小及颜色

之前的程序做了一个可以输入的消息弹出框,样式如下:

那么问题就来了,我现在想修改一下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];


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容