開發(fā)中遇到了這個小問題,網上找了N多種方法谜诫,沒有合意的漾峡,所以參照網上的簡單改了一版,既能用aletr左對齊喻旷,又不會讓文字太過得靠左和靠下生逸。
NSString *msgStr = @" 尊敬的用戶朋友,由于%@渠道需進行系統(tǒng)升級維護且预,所以目前暫不支持該項服務槽袄。\n 敬請關注系統(tǒng)消息,服務恢復時間會進行及時公告锋谐。\n 客服咨詢熱線:4000000000";
1.需要適配iOS7以下的遍尺,可以用alert(我最喜歡這種方式,因為他可以在NSObject中彈框)
_alertView = [[UIAlertView alloc] initWithTitle:@"溫馨提示" message:msgStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"撥打電話", nil];
UIView *view = [[UIView alloc]init];
UILabel *textLabel = [[UILabel alloc] init];
textLabel.font = [UIFont systemFontOfSize:15];
textLabel.textColor = [UIColor blackColor];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.numberOfLines =0;
textLabel.textAlignment = NSTextAlignmentLeft;
textLabel.text = msgStr;
[view addSubview:textLabel];
[textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.right.equalTo(view);
make.bottom.equalTo(view.mas_bottom).offset(-10);
make.left.equalTo(view.mas_left).offset(5);
}];
[_alertView setValue:view forKey:@"accessoryView"];
_alertView.message =@"";
[_alertView show];
2.iOS8以上涮拗,可以用UIAlertController乾戏,但在NSObject中不能封裝
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"溫馨提示:"message:msgStr 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];
//title.textAlignment = NSTextAlignmentLeft;
UILabel *message = subView5.subviews[1];
message.textAlignment = NSTextAlignmentLeft;
[alert addAction: [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction: [UIAlertAction actionWithTitle:@"撥打電話" style:UIAlertActionStyleDefault handler:^(UIAlertAction*action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://4000000000"]];
}]];