更新UIAlertView消息dynamic和换行符问题

我需要在我的UIAlertView的消息中显示多行文本。 我已经尝试添加一个'\ n',但它没有效果。 它仍然显示:“这是一个例子….”。

但是,如果我把我的iPhone到横向模式,它会显示消息,因为我打算。 然后如果我切换回肖像模式,它也显示正确。

更新:经过进一步的考虑,我怀疑这是与我正在更新当前消息与一个新的(更长的)string的事实。 我已经在alert视图上调用了“show”,并试图更新消息。 也许有些东西需要重新绘制? 就像我之前说过的,如果我改变方向(无论哪个方向我开始,我仍然有同样的问题)它显示正确。 我已经尝试过“setNeedsDisplay”和“setNeedsLayout”。

虽然我相信在显示时更新警报视图的文本是错误的,但我认为改变它的唯一方法就是这样

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:@"this is a message\nthis is a new line text" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"test button",nil]; [alert show]; //set the new message NSString *newMessage = [NSString stringWithString:@"Test\nWith a new message with\ncustom new line string\n and a lot\n of new lines\n Yep"]; [alert setMessage:newMessage]; //get the original alert Frame CGRect alertFrame = alert.frame; //get the alert view Label UILabel *alertLabel = [alert.subviews objectAtIndex:2]; //and get the original frame position CGRect alertLabelFrame = alertLabel.frame; //see how much space we are needing for the new message and adjust int heightChange = [newMessage sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(alertLabelFrame.size.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height - alertLabelFrame.size.height; alertFrame.size.height += heightChange; //adjust the Label height to the new height alertLabelFrame.size.height += heightChange; //adjust the frame and elements with an animation for smoothness [UIView animateWithDuration:0.15 delay:0.4 options:(UIViewAnimationCurveEaseIn) animations:^{ [alert setFrame:alertFrame]; alertLabel.frame = alertLabelFrame; //move any buttons for (UIView *subView in alert.subviews) { if ([subView isKindOfClass:[UIButton class]]) { //this is a button move it UIButton *button = (UIButton*)subView; CGRect alertButtonFrame = button.frame; //adjust button Y position to the new height alertButtonFrame.origin.y += heightChange-5; button.frame = alertButtonFrame; } } } completion:nil]; 

对于换行符,请在文本中使用\ n换行符:

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:@"this is a message\nthis is a new line text" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; 

即使使用setMessage这个工作:

 UIAlertView *alert = [[UIAlertView alloc] init]; [alert setMessage:@"this is\na test"]; [alert show]; [alert release]; 

使用'\ r'符号。 它应该正确地断线。

这里有一些简单的代码来调整警报中的消息大小。 请注意,警报框架需要足够大。 您可以使用几个换行符来填充最初的警报。

 for (UILabel *l in alertView.subviews){ if ([l isKindOfClass:[UILabel class]]){ float w = l.frame.size.width; [l setNumberOfLines:0]; [l sizeToFit]; CGRect r = l.frame; r.size.width = w; l.frame = r; } } 
  1. 创build您自己的AlertView类别。

AlertWithBlock.h文件

 #import <UIKit/UIKit.h> @interface UIAlertView (AlertWithBlock) - (void (^)(UIAlertView *alertView, NSInteger buttonIndex))delegateBlock; - (void)setDelegateBlock:(void (^)(UIAlertView *alertView, NSInteger buttonIndex))delegateBlock; + (void)alertViewWithTitle:(NSString *)title buttonTitle:(NSString *)buttonTitle message:(NSString *)message; + (UIAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles delegate:(void (^)(UIAlertView *alertView, NSInteger buttonIndex))delegate; @end