我知道这是一个老问题,但我遇到了同样的问题,这里的答案对我有帮助。
我需要放大标题和消息文本字段以便在 iPad 上容纳更多文本。
我所做的是实现 UIAlertView willPresentAlertView: 委托方法并添加两个 UILabel 对象作为标题和消息。我能够配置这两个标签的帧大小和原点。在获得文本字段的所需大小后,我调整了警报视图的大小以适应新文本。然后我遍历alertview的子视图以找到按钮并调整它的框架。
- (void)willPresentAlertView:(UIAlertView *)alertView {
// add a new label and configure it to replace the title
UILabel *tempTitle = [[UILabel alloc] initWithFrame:CGRectMake(10,20,350, 20)];
tempTitle.backgroundColor = [UIColor clearColor];
tempTitle.textColor = [UIColor whiteColor];
tempTitle.textAlignment = UITextAlignmentCenter;
tempTitle.numberOfLines = 1;
tempTitle.font = [UIFont boldSystemFontOfSize:18];
tempTitle.text = alertView.title;
alertView.title = @"";
[alertView addSubview:tempTitle];
[tempTitle release];
// add another label to use as message
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 350, 200)];
tempLabel.backgroundColor = [UIColor clearColor];
tempLabel.textColor = [UIColor whiteColor];
tempLabel.textAlignment = UITextAlignmentCenter;
tempLabel.numberOfLines = 20;
tempLabel.text = alertView.message;
[alertView addSubview:tempLabel];
// method used to resize the height of a label depending on the text height
[self setUILabel:tempLabel withMaxFrame:CGRectMake(10,50, 350, 300) withText:alertView.message];
alertView.message = @"";
// set the frame of the alert view and center it
alertView.frame = CGRectMake(CGRectGetMinX(alertView.frame) - (370 - CGRectGetWidth(alertView.frame))/2 ,
alertView.frame.origin.y,
370,
tempLabel.frame.size.height + 120);
// iterate through the subviews in order to find the button and resize it
for( UIView *view in alertView.subviews)
{
if([[view class] isSubclassOfClass:[UIControl class]])
{
view.frame = CGRectMake (view.frame.origin.x+2,
tempLabel.frame.origin.y + tempLabel.frame.size.height + 10,
370 - view.frame.origin.x *2-4,
view.frame.size.height);
}
}
[tempLabel release];
}
以及用于调整标签大小的方法:
- (void)setUILabel:(UILabel *)myLabel withMaxFrame:(CGRect)maxFrame withText:(NSString *)theText{
CGSize stringSize = [theText sizeWithFont:myLabel.font constrainedToSize:maxFrame.size lineBreakMode:myLabel.lineBreakMode];
myLabel.frame = CGRectMake(myLabel.frame.origin.x,
myLabel.frame.origin.y,
myLabel.frame.size.width,
stringSize.height
);
}
我不知道这是否是最好的方法,但它对我有用。