【问题标题】:Updating UIAlertView Message dynamically and newline character issue动态更新 UIAlertView 消息和换行符问题
【发布时间】:2012-04-19 20:25:17
【问题描述】:

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

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

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

【问题讨论】:

  • 为什么在显示对话框后更改消息?当然是你的问题
  • 我正在使用对话框让用户了解任务的进度。有没有更好的方法来做到这一点?

标签: objective-c ios uialertview


【解决方案1】:

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

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];

【讨论】:

  • 我现在正在做类似上面的事情,但有点不同......你能帮我快速聊天吗?
  • @KeeanoMartin 我建议你把它作为一个新问题提出来,我或其他人可以回复
【解决方案2】:

对于换行符,请在文本中使用 \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];

【讨论】:

  • 如果我这样做:[myAlertView setMessage:@"This is my\nmessage"] 它将无法正常工作。
  • 您是否尝试复制粘贴我写的内容?我刚刚在模拟器中试了一下,效果很好……\r\n也可以试试
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    这里有一些简单的代码来调整警报中的消息大小。请注意,警报的框架需要足够大。您可以在初始警报中添加几行换行符。

    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;
        }
    }
    

    【讨论】:

      【解决方案5】:
      1. 创建您自己的 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
      

      【讨论】:

      • #import "UIAlertView+AlertWithBlock.h" #import
      • 在Controller.m文件中导入
      • 如何从控制器调用。[UIAlertView alertViewWithTitle:@"Info!" buttonTitle:@“取消”消息:@“网络错误!”]; [UIAlertView alertWithTitle:@"Title" message:@"This is a message" cancelButtonTitle:nil otherButtonTitles:@[@"Yes",@"No"] 委托:^(UIAlertView *alertView, NSInteger buttonIndex) { [self showButtonIndex:按钮索引]; }]; }];
      • 我对 Objective-C 了解不多,但这显然不是您必须在该文件中编写的内容。您能否改进格式以使其更具可读性?尤其要注意使用代码块(如果那是实际代码),将代码行缩进 4 个空格。
      猜你喜欢
      • 1970-01-01
      • 2013-10-30
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      相关资源
      最近更新 更多