我需要在UIAlertView的消息中显示多行文本。我尝试添加'\ n',但它没有效果。它仍然显示:“这是一个例子......”。
但是,如果我将iPhone转为横向模式,它会显示我想要的消息。然后,如果我将BACK切换到纵向模式,它也会在那里正确显示。
更新:经过进一步考虑后,我怀疑它与我用新的(更长的)字符串更新当前消息这一事实有关。我已经在警报视图上调用了“show”,并且我正在尝试更新消息。也许有些东西需要重新绘制?就像我之前说的那样,如果我改变方向,它会正确显示(无论我开始哪个方向,我仍然有同样的问题)。我已经尝试过“setNeedsDisplay”和“setNeedsLayout”。
答案 0 :(得分:6)
虽然我认为更新警报视图的文本在显示时是错误的,但我看到改变它的唯一方法就是这样
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];
答案 1 :(得分:3)
对于换行符,请在文本中使用\ 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];
答案 2 :(得分:0)
使用' \ r'符号。它应该正确地断线。
答案 3 :(得分:0)
以下是一些调整警报中消息大小的简单代码。请注意,警报的框架需要足够大。您可以使用一些换行符填充初始警报。
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;
}
}
答案 4 :(得分:-2)
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