直接上代碼:
UIAlertController *alertCtr = [UIAlertController alertControllerWithTitle:@"規(guī)則說(shuō)明" message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
//改變title的大小和顏色
NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:title];
[titleAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, title.length)];
[titleAtt addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, title.length)];
[alertController setValue:titleAtt forKey:@"attributedTitle"];
//改變message的大小和顏色
NSMutableAttributedString *messageAtt = [[NSMutableAttributedString alloc] initWithString:message];
[messageAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, message.length)];
[messageAtt addAttribute:NSForegroundColorAttributeName value:[UIColor darkTextColor] range:NSMakeRange(0, message.length)];
[alertController setValue:messageAtt forKey:@"attributedMessage"];
//修改按鈕的顏色,同上可以使用同樣的方法修改內(nèi)容,樣式
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"];
// UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
// }];
[alertCtr addAction:cancleAction];
// [alertCtr addAction:okAction];
UIView *subView1 = alertCtr.view.subviews[0];
UIView *subView2 = subView1.subviews[0];
UIView *subView3 = subView2.subviews[0];
UIView *subView4 = subView3.subviews[0];
UIView *subView5 = subView4.subviews[0];
// UILabel *title = subView5.subviews[0];//第一個(gè)是標(biāo)題,
//第二個(gè)是message
UILabel *message = subView5.subviews[1];
//改變message的對(duì)齊方式
message.textAlignment = NSTextAlignmentLeft;
[self presentViewController:alertCtr animated:YES completion:nil];