How to align only the title in UIAlertView

对着背影说爱祢 提交于 2019-12-12 03:52:08

问题


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

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