2

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

但是,如果我将 iPhone 转为横向模式,它会按照我的意图显示消息。然后,如果我切换回纵向模式,它也会在那里正确显示。

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

4

5 回答 5

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];
于 2012-04-23T21:47:35.633 回答
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];
于 2012-04-19T20:30:23.273 回答
0

使用“\r”符号。它应该正确地换行。

于 2012-04-19T20:50:22.363 回答
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;
    }
}
于 2013-05-02T01:18:36.373 回答
-2
  1. 创建您自己的警报视图类别。

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
于 2016-08-03T12:45:06.327 回答