Updating UIAlertView Message dynamically and newline character issue

偶尔善良 提交于 2019-12-20 03:21:47

问题


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

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.

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


回答1:


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



回答2:


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



回答3:


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




回答4:


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



回答5:


  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


来源:https://stackoverflow.com/questions/10236167/updating-uialertview-message-dynamically-and-newline-character-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!