开发中遇到了这个小问题,网上找了N多种方法,没有合意的,所以参照网上的简单改了一版,既能用aletr左对齐,又不会让文字太过得靠左和靠下。
NSString *msgStr = @" 尊敬的用户朋友,由于%@渠道需进行系统升级维护,所以目前暂不支持该项服务。\n 敬请关注系统消息,服务恢复时间会进行及时公告。\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"]];
}]];