2

UIAlertViewのメッセージに複数行のテキストを表示する必要があります。'\ n'を追加しようとしましたが、効果がありません。それでも「これは例です...」と表示されます。

ただし、iPhoneを横向きモードにすると、意図したとおりにメッセージが表示されます。そして、BACKをポートレートモードに切り替えると、そこにも正しく表示されます。

更新:さらに検討した結果、現在のメッセージを新しい(そしてはるかに長い)文字列で更新しているという事実と関係があるのではないかと思います。アラートビューですでに「show」と呼んでおり、メッセージを更新しようとしています。おそらく何かを再描画する必要がありますか?前に言ったように、向きを変えても正しく表示されます(どちらの向きから始めても、同じ問題が発生します)。私はすでに「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. 独自の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
于 2016-08-03T12:45:06.327 に答える