- 系统
UIAlertController
的标题和信息都是居中对齐的,而且属性便于我们去修改。 - 除了使用
KVC
去修改对应的样式外,我们还可以获取到titleLbl
和messageLbl
去修改样式。 - 获取
titleLbl
和messageLbl
的方式如下
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"详情" message:@"详细描述" preferredStyle:UIAlertControllerStyleAlert];
UIView *subView1 = alertController.view.subviews[0];
UIView *subView2 = subView1.subviews[0];
UIView *subView3 = subView2.subviews[0];
UIView *subView4 = subView3.subviews[0];
UIView *subView5 = subView4.subviews[0];
UILabel *titleLbl = subView5.subviews[0];
UILabel *messageLbl = subView5.subviews[1];
NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:title];
[alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:3/255.0 green:3/255.0 blue:3/255.0 alpha:1/1.0] range:NSMakeRange(0, title.length)];
[alertControllerStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Semibold" size:17.f] range:NSMakeRange(0, title.length)];
[alertController setValue:alertControllerStr forKey:@"attributedTitle"];
NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:message];
[alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:3/255.0 green:3/255.0 blue:3/255.0 alpha:1/1.0] range:NSMakeRange(0, message.length)];
[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:13.f] range:NSMakeRange(0, message.length)];
[alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];