UIAlertController message,title居左以及其他调整

实例化alertcontroller之后,可以遍历view的子视图,子视图title和message嵌套了多层父视图,(需要遍历6层subViews),

下面是取title和message的父视图的代码:

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;

swift版

let alert =UIAlertController(title:"",message:msg,preferredStyle: .alert)

let subView = alert.view.subviews[0].subviews[0].subviews[0].subviews[0].subviews[0]

 let msgLabel = subView.subviews[2] as?UILabel

msgLabel?.textAlignment= .left


let msgLabel = subView.subviews[1] as?UILabel则取的是title

let msgLabel = subView.subviews[2] as?UILabel则取的是message



    //修改title样式

    NSMutableAttributedString *titleStr = [[NSMutableAttributedString alloc] initWithString:@"卖家联系方式"];

    [titleStr addAttribute:NSForegroundColorAttributeName value:LRRGBColor(142, 142, 147) range:NSMakeRange(0, titleStr.length)];

    [titleStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17 weight:UIFontWeightMedium] range:NSMakeRange(0, titleStr.length)];

    [con setValue:titleStr forKey:@"attributedTitle"];

//修改message样式 

        let leftStr = NSAttributedString.init(string: "msg1:", attributes: [NSAttributedString.Key.foregroundColor : UIColor.gray , NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14)])

           let rightStr = NSAttributedString.init(string: msg2, attributes: [NSAttributedString.Key.foregroundColor : UIColor.black , NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 14)])

           let attributedStrM : NSMutableAttributedString = NSMutableAttributedString()

           attributedStrM.append(leftStr)

           attributedStrM.append(rightStr)

           alert.setValue(attributedStrM,forKey:"attributedMessage")


//按钮颜色

    UIAlertAction * cancleAciton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

    [cancleAciton setValue:LRRGBColor(142, 142, 147) forKey:@"_titleTextColor"]; 

如果有用,请点个赞❤️再走吧 么么哒

你可能感兴趣的:(UIAlertController message,title居左以及其他调整)