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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11571315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:19:45  来源:igfitidea点击:

How to make a multiple line, left-aligned UIAlertView?

iosobjective-csdkuialertview

提问by DavidNg

I am interested in making a left-aligned UIAlertView with several lines like a bulletin list that would look like the following:

我有兴趣制作一个左对齐的 UIAlertView,其中有几行类似于公告列表,如下所示:

  • line 1
  • line 2
  • line 3
  • 1号线
  • 2号线
  • 3号线

Here's what I have so far:

这是我到目前为止所拥有的:

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

I also want to change the color of the alert view to red.

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

回答by pasawaya

Bullets are represented by the unicode code 0x2022. I got it to work like this using "\n" for new lines:

项目符号由 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];

That should work for the bullets.

这应该适用于子弹。

For the left alignment, do this:

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

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;
    }
}

So in summary:

所以总结一下:

  • "\n" for displaying new lines.
  • [NSString stringWithFormat:@"%C Line n", (unichar) 0x2022];for the bullets.
  • For the alignment, iterate through the subviews of the alert view and identify which subviews are subclasses of UILabel. Then change the text alignment of the labels to left alignment using label.textAlignment = NSTextAlignmentLeft.
  • After you do allthis, then you can call [alert show];.
  • "\n" 用于显示新行
  • [NSString stringWithFormat:@"%C Line n", (unichar) 0x2022];对于子弹
  • 对于对齐,遍历警报视图的子视图并确定哪些子视图是 的子类UILabel。然后使用 将标签的文本对齐方式更改为左对齐方式label.textAlignment = NSTextAlignmentLeft
  • 完成所有这些之后,您就可以调用[alert show];.

回答by JSA986

to align left:

左对齐:

 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;
    }

To show a red background behind alert view:

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

alert.backgroundColor=[UIColor redColor];

回答by hEgg

Make sure you use the \ and not the normal /.

确保使用 \ 而不是普通的 /。

alt + shift + 7 (on my Keyboard)

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

\n

\n

That should do it. :-)

那应该这样做。:-)

回答by Michael Frederick

You can create a new line by inserting \ninto your message. For example:

您可以通过插入\n到您的消息中来创建一个新行。例如:

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

回答by The iOSDev

For the new line and text alignment prefer the @qegal'sanswer that one is perfect but if you like to change the color or customize the alert view I got one great class to use instead of system's alert view just check thisout, this will definitely works. even it will also helps you to get multi-lined message with alignment and bulleted list.

新生产线和文本对齐喜欢@ qegal的答案,一个是完美的,但如果你想改变颜色或自定义警报视图我有一个伟大的阶级,而不是使用系统的警报视图只检查这个出去,这肯定会工作. 甚至它还可以帮助您获得带有对齐和项目符号列表的多行消息。

If you found any problem with using it ask for help, I will surely help you out.

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

Happy Coding :)

快乐编码:)