【问题标题】:How to align only the title in UIAlertView如何仅对齐 UIAlertView 中的标题
【发布时间】:2013-03-25 05:20:08
【问题描述】:
我想在 UIAlertView 中仅将标题居中对齐,而消息应该左对齐。
目前我在做
((UILabel*)[[alert subviews] objectAtIndex:1]).textAlignment = NSTextAlignmentCenter;
但它也使消息居中对齐。
【问题讨论】:
-
这是一种非常危险的访问子视图的方式。如果您真的想自定义警报视图的外观,您可能想要制作自己的自定义警报视图。试试this教程。
标签:
iphone
objective-c
cocoa-touch
uiimageview
【解决方案1】:
我不会尝试修改 UIAlertView 的默认标签,而是应该创建一个新的 UILabel 并将其添加为子视图。
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];