iOS 学习笔记 -- UIAlertController 自定义
程序员文章站
2022-04-26 23:28:30
...
在平常的开发中,经常会用到UIAlertController,用的时候系统自带的样式不符合我们的需求,需要我们自己定义UIAlertController 标题和内容的文本样式。在这里我们通过kvc的思想来实现。
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];
打印的信息为:
在 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,为所需的内容进行设置样式。
仅个人见解,如有错误请见谅!