iOS Alert左对齐

Alert文本文字默认是中心对齐的,于是我用笨办法写了个可以左对齐的方法,效果图先贴出来,接着上代码.

iOS Alert左对齐_第1张图片
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"加班狗 更新提示!" message:@"1. 修复了熬夜两天还能继续嗨的bug\n2. 精力恢复速度下调\n3. 转眼往事几率大幅提高" preferredStyle:UIAlertControllerStyleAlert];

    UIView *subView1 = alert.view.subviews[0];
    UIView *subView2 = subView1.subviews[0];
    UIView *subView3 = subView2.subviews[0];
    UIView *subView4 = subView3.subviews[0];
    UIView *subView5 = subView4.subviews[0];
    
    //分别拿到title 和 message 可以分别设置他们的对齐属性
//    UILabel *title = subView5.subviews[0];
    UILabel *message = subView5.subviews[1];
    
    message.textAlignment = NSTextAlignmentLeft;
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancelAction];

    [self presentViewController:alert animated:YES completion:nil];
}

你可能感兴趣的:(iOS Alert左对齐)