2012-04-19 31 views
2

我需要在我的UIAlertView的消息中顯示多行文本。我曾嘗試添加'\ n',但它沒有效果。它仍然顯示:「這是一個例子....」。然而,如果我將我的iPhone變成橫向模式,它會按照我的意圖顯示消息。然後如果我切換回肖像模式,它也會正確顯示。更新UIAlertView消息動態和換行符問題

更新:經過進一步的考慮,我懷疑它與我正在用新(更長的)字符串更新當前消息的事實有關。我已經在alert視圖上調用了「show」,並試圖更新消息。也許有些東西需要重新繪製?就像我以前說過的那樣,如果我改變方向,它會正確顯示(不管我從哪個方向開始,我仍然有同樣的問題)。我已經嘗試過「setNeedsDisplay」和「setNeedsLayout」。

+0

你爲什麼改變了消息之後顯示的對話框?當然你的問題在那裏 – Lefteris 2012-04-23 12:38:56

+0

我使用對話框來保持用戶更新任務的進度。有沒有更好的方法來做到這一點? – Groppe 2012-04-23 13:04:49

回答

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

即時工作在上面這樣的事情上,但有點不同....你能真正快速地幫我出去聊天嗎? – Keeano 2013-07-12 03:30:27

+0

@KeeanoMartin我建議你只是問這是一個新的問題,我或其他人可以回覆 – Lefteris 2013-07-12 13:51:18

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

如果我這樣做:[myAlertView setMessage:@「這是我的\ n消息」]它無法正常工作。 – Groppe 2012-04-19 20:39:46

+0

它工作正常,我看到我的編輯 – Lefteris 2012-04-19 20:45:07

+0

由於某種原因,它不適合我... – Groppe 2012-04-19 20:48:01

0

使用 '\ r' 符號。它應該正確斷線。

+0

這也行不通。 – Groppe 2012-04-22 02:46:20

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; 
    } 
} 
-2
  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 
+0

#import「UIAlertView + AlertWithBlock.h」 #import 2016-08-03 12:51:33

+0

導入它在控制器.m文件 – 2016-08-03 12:51:49

+0

如何從控制器調用[UIAlertView alertViewWithTitle:@「Info!」 buttonTitle:@「Cancle」消息:@「網絡錯誤!「]; [UIAlertView alertWithTitle:@」Title「message:@」This is a message「cancelButtonTitle:nil otherButtonTitles:@ [@」Yes「,@」No「] delegate:^(UIAlertView * alertView,NSInteger buttonIndex) { [self showButtonIndex:buttonIndex]; }]; }]; – 2016-08-03 12:53:46