實例化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"];?
如果有用俯艰,請點個贊??再走吧 么么噠