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

别来无恙 提交于 2019-11-28 16:51:37

问题


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

  • line 1
  • line 2
  • line 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.


回答1:


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

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 all this, then you can call [alert show];.



回答2:


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



回答3:


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

alt + shift + 7 (on my Keyboard)

\n

That should do it. :-)




回答4:


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

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



回答5:


For the new line and text alignment prefer the @qegal's answer 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 this out, this will definitely works. even it will also helps you to get multi-lined message with alignment and bulleted list.

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

Happy Coding :)



来源:https://stackoverflow.com/questions/11571315/how-to-make-a-multiple-line-left-aligned-uialertview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!