如何制作多行,左对齐的UIAlertView?

Dav*_*dNg 18 sdk objective-c uialertview ios

我有兴趣制作一个左对齐的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];
Run Code Online (Sandbox Code Playgroud)

我还想将警报视图的颜色更改为红色.

pas*_*aya 31

项目符号由unicode代码0x2022表示.我使用"\n"为新行开始这样工作:

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];
Run Code Online (Sandbox Code Playgroud)

这应该适用于子弹.

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

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

总结如下:

  • "\n"用于显示新行.
  • [NSString stringWithFormat:@"%C Line n", (unichar) 0x2022];对于子弹.
  • 对于对齐,迭代警报视图的子视图并确定哪些子视图是子类UILabel.然后使用标签将标签的文本对齐方式更改为左对齐label.textAlignment = NSTextAlignmentLeft.
  • 完成所有这些后,您就可以打电话了[alert show];.

  • @pasawaya左对齐的代码似乎不起作用.首先,我假设您的意思是NSArray*subViewArray = alert.subviews; 因为alertView.subviews不存在.其次,当我在初始化alert之后放置此代码时,subViewArray具有零对象.我错过了什么吗? (3认同)
  • 此外,在执行项目符号时,您需要对十六进制代码进行类型转换以防止Xcode显示警告:`[NSString stringWithFormat:@"%C Line n",(unichar)0x2022] (2认同)