问题
I would like to align center only the title in UIAlertView, while the message should be align left.
Currently I am doing
((UILabel*)[[alert subviews] objectAtIndex:1]).textAlignment = NSTextAlignmentCenter;
But it aligns center also the message.
回答1:
I wouldn't try to modify the default labels of the UIAlertView, instead you should create a new UILabel and add it as a subview.
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];
来源:https://stackoverflow.com/questions/15789098/how-to-align-only-the-title-in-uialertview