Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 859
DavidNg Avatar asked Jul 20 '12 00:07

DavidNg


1 Answers

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];.
like image 167
pasawaya Avatar answered Oct 12 '22 01:10

pasawaya