如何使一个多行,左alignment的UIAlertView?

我有兴趣做一个左alignment的UIAlertView像几个公告列表,看起来像下面这样:

  • 第1行
  • 2号线
  • 第3行

以下是我到目前为止:

alert = [[UIAlertView alloc] initWithTitle: @"How to use buttons" message: @"line 1. line 2, line 3 " delegate: nil cancelButtonTitle: @"OK" otherButtonTitles: nil]; 

我也想改变警报视图的颜色为红色。

项目符号由unicode代码0x2022表示。 我使用“\ n”replace了新行:

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"How to use buttons" message: [NSString stringWithFormat: @"%C line 1.\n %C line 2,\n %C line 3", (unichar) 0x2022, (unichar) 0x2022, (unichar) 0x2022]; delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

这应该适用于子弹。

对于左alignment,请执行以下操作:

 NSArray *subViewArray = alertView.subviews; for(int x = 0; x < [subViewArray count]; x++){ //If the current subview is a UILabel... if([[[subViewArray objectAtIndex:x] class] isSubclassOfClass:[UILabel class]]) { UILabel *label = [subViewArray objectAtIndex:x]; label.textAlignment = NSTextAlignmentLeft; } } 

总之:

  • “\ n”用于显示新行
  • [NSString stringWithFormat:@"%C Line n", (unichar) 0x2022];子弹
  • 为了alignment ,遍历alert视图的子视图,并确定哪些子视图是UILabel子类。 然后使用label.textAlignment = NSTextAlignmentLeft将标签的文本alignment更改为左alignment。
  • 当你做完这一切后,你可以打电话给[alert show];

左alignment:

  NSArray *subviewArray = alert.subviews; for(int x = 0; x < [subviewArray count]; x++){ if([[[subviewArray objectAtIndex:x] class] isSubclassOfClass:[UILabel class]]) { UILabel *label = [subviewArray objectAtIndex:x]; label.textAlignment = UITextAlignmentLeft; } 

在警报视图后显示红色背景:

 alert.backgroundColor=[UIColor redColor]; 

确保你使用\而不是正常/。

alt + shift + 7(在我的键盘上)

\ n

这应该做到这一点。 🙂

你可以在你的信息中插入\n来创build一个新行。 例如:

 @"line 1.\nline 2,\nline 3" 

对于新的行和文本alignment喜欢@ qegal的答案,一个是完美的,但如果你想改变颜色或自定义警报视图,我有一个伟大的类,而不是系统的警报视图只是检查了这一点 ,这一定会起作用。 即使它也可以帮助你获得alignment和项目符号列表的多行消息。

如果您发现使用它的任何问题请求帮助,我一定会帮助您。

快乐编码:)