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


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容