Reputation: 431
When I use UIAlertView,I find the message's textAligment is Center. e.g.,When I write this code:
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
message:@"this is first line \nsecondLine should be center"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil, nil];
[alertView show];
the effect is follow:
As we see, the text"this is first line" is in the center,but I want to change it to left.I know UILabel can change text's alignment,but I don't know whether can I change it in UIAlertView.
Upvotes: 1
Views: 2680
Reputation: 10327
iOS7: For left alignment, you can 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;
}
}
Ref: How to make a multiple line, left-aligned UIAlertView?
Or you can add an UILabel to alertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Centered Title" message:@"\n\n\n" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12.0, 24.0, 250.0, 80.0)];
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = @"Here is an example of a left aligned label in a UIAlertView!";
[alert addSubview:label];
[alert show];
Ref: How to align only the title in UIAlertView
Upvotes: 1
Reputation: 2999
Edit your code like this.
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil, nil];
UILabel *v = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
v.text = NSTextAlignmentLeft;
v.numberOfLines = 0;
v.text = @"this is first line\nsecondLine should be center";
[alert setValue:v forKey:@"accessoryView"];
[alert show];
Upvotes: 2