动态更新UIAlertView消息和换行符问题 [英] Updating UIAlertView Message dynamically and newline character issue
问题描述
我需要在UIAlertView的消息中显示多行文本。我试过添加一个'\ n',但它没有效果。它仍然显示:这是一个例子......。
I need to display multiple lines of text in the message of my UIAlertView. I have tried adding a '\n', but it has no effect. It still displays: "This is an examp....".
但是,如果我将我的iPhone转为横向模式,它会显示我想要的消息。然后,如果我切换回纵向模式,它也会在那里正确显示。
HOWEVER, if I turn my iPhone to landscape mode it displays the message as I intend it to. And then if I switch BACK to portrait mode it displays correctly there as well.
更新:经过进一步考虑后,我怀疑它与我的事实有关使用新的(更长的)字符串更新当前消息。我已经在警报视图中调用了show,并且我正在尝试更新消息。也许有些东西需要重新绘制?就像我之前说的,如果我改变方向,它会正确显示(无论我开始哪个方向,我仍然有同样的问题)。我已经尝试了setNeedsDisplay和setNeedsLayout。
Update: After further consideration, I suspect it has something to do with the fact that I am updating the current message with a new (and much longer) string. I have already called "show" on the alert view, and am trying to update the message. Perhaps something needs to be redrawn? Like i said before, it displays correctly if I change orientations (doesn't matter which orientation I start in, i still have the same problem). i have already tried "setNeedsDisplay" and "setNeedsLayout".
推荐答案
虽然我认为在显示警报视图的文本时更新它是错了,我看到改变它的唯一方法就是这样
Although I believe updating the alert view's text while it's being displayed is wrong, the only way I see to change it is this way
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];
这篇关于动态更新UIAlertView消息和换行符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!