在平常的开发中,经常会用到UIAlertController,用的时候系统自带的样式不符合我们的需求,需要我们自己定义UIAlertController 标题和内容的文本样式。在这里我们通过kvc的思想来实现。在这里补充一点:在使用中发现这个方法只适用于iOS 12的系统,其他系统具体看UIAlertController的层级结构。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"这是标题" message:@"这是内容。\n1、这是内容一。\n2、这是内容二。\n3、这是内容三。\n4、这是内容四。" preferredStyle:(UIAlertControllerStyleAlert)]; UIView *subView1 = alert.view.subviews[0].subviews[0].subviews[0].subviews[0].subviews[0];
NSLog(@"==========>%@",subView1.subviews); UILabel * tmpMessageLabel =subView1.subviews[2]; [tmpMessageLabel setTextAlignment:NSTextAlignmentLeft]; UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:cancelAction]; [alert addAction:okAction]; [self presentViewController:alert animated:YES completion:nil];
|
打印的信息为:
1 2 3 4 5 6 7 8
| 在 subView5.subviews 中的内容 ( "<UIView: 0x10fc01380; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x282037d60>>", "<UILabel: 0x10fc01560; frame = (0 0; 0 0); text = '\U63d0\U73b0\U89c4\U5219'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x28033dc70>>", "<UILabel: 0x10fc01850; frame = (0 0; 0 0); text = '1\U3001\U63d0\U73b0\U91d1\U989d\U9700\U5728255.00\U5143\U4ee5\U4e0a\Uff0c\U4e0d\U8db3255.0...'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x28033ec10>>", "<UIView: 0x10fc01b40; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x28202b0e0>>", "<UIView: 0x10fc01d20; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x28202b120>>" )
|
在这里就找到了标题和内容所对应的label,为所需的内容进行设置样式。
仅个人见解,如有错误请见谅!