最近将公司测试机更新到iOS 12 btea5版本,好提前作下适配。
其中遇到一个小问题,原本在iOS11中UIAlertController中Message文字属性设置却在iOS 12中出现了显示问题。
这里先放上需求方案的 UIAlertController
:
message内容需要居左显示。
再放上一张iOS 12 中的 UIAlertController
:
再看看代码
NSString *words = @"· You will earn Rs.100 Generic when your friend signs up on ******** by your referral links. \n· You will earn Rs.200 for every booking of your referred friend.";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Share your \"phone book\" contacts" message:words 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];
NSLog(@"%@",subView5.subviews);
//取title和message:
UILabel *title = subView5.subviews[0];
UILabel *messageLabel = subView5.subviews[1];
messageLabel.textAlignment = NSTextAlignmentLeft;
NSMutableAttributedString *attributeText = [[NSMutableAttributedString alloc]initWithString:words];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineSpacing = 3;
paragraphStyle.firstLineHeadIndent = 0;
paragraphStyle.paragraphSpacing = 10;
[attributeText addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [words length] - 1)];
messageLabel.attributedText = attributeText;
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
因为项目是swift,无法打印subView5.subviews的包含视图,所以特地用oc写了一遍
iOS 11 UIAlertController.png
在iOS12之前
UIAlertController
的主体层次包含四个视图,反观iOS 12中结果给了我点小惊喜iOS 12 UIAlertController subviews.png iOS 12 UIAlertController.png
图层不难看出subviews[0]多出一个UIView,对它做一系列操作都没看到啥效果(闹呢)
一般这种需求,自定义的话会舒服很多
网友评论