Updating UIAlertView Message dynamically and newline character issue

前端 未结 5 1581
萌比男神i
萌比男神i 2021-01-22 00:21

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....\".

相关标签:
5条回答
  • 2021-01-22 00:58

    Here is some simple code to resize the messages in an alert. Note that the alert's frame needs to be large enough. You can pad the initial alert with a few newlines.

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-22 01:04

    Use '\r' symbol. It should break line properly.

    0 讨论(0)
  • 2021-01-22 01:06

    For newline use the \n newline character in your text like this:

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

    Even with setMessage this works:

    UIAlertView *alert = [[UIAlertView alloc] init];
    [alert setMessage:@"this is\na test"];
    [alert show];
    [alert release];
    
    0 讨论(0)
  • 2021-01-22 01:08
    1. Create your own AlertView Category.

    AlertWithBlock.h file

    #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
    
    0 讨论(0)
  • 2021-01-22 01:13

    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];
    
    0 讨论(0)
提交回复
热议问题