簡體   English   中英

如何在UIAlertView中更改消息的對齊?

[英]How to change message's aligment in UIAlertView?

當我使用UIAlertView時,我發現消息的textAligment是Center。 例如,當我寫這段代碼時:

    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
                                                   message:@"this is first line     \nsecondLine should be center"
                                                  delegate:nil
                                         cancelButtonTitle:nil
                                         otherButtonTitles:nil, nil];
[alertView show];

效果如下: 在此輸入圖像描述
正如我們所看到的,文本“這是第一行”位於中心,但我想將其更改為left.I知道UILabel可以更改文本的對齊方式,但我不知道是否可以在UIAlertView中更改它。

像這樣編輯你的代碼。

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil
                                               message:nil
                                              delegate:nil
                                     cancelButtonTitle:nil
                                     otherButtonTitles:nil, nil];


UILabel *v = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
v.text = NSTextAlignmentLeft;
v.numberOfLines = 0;
v.text = @"this is first line\nsecondLine should be center";
[alert setValue:v forKey:@"accessoryView"];
[alert show];

iOS7:對於左對齊,您可以這樣做:

NSArray *subViewArray = alertView.subviews;
for(int x = 0; x < [subViewArray count]; x++){

    //If the current subview is a UILabel...
    if([[[subViewArray objectAtIndex:x] class] isSubclassOfClass:[UILabel class]]) {
        UILabel *label = [subViewArray objectAtIndex:x];
        label.textAlignment = NSTextAlignmentLeft;
    }
}

參考: 如何制作多行,左對齊的UIAlertView?

或者您可以將UILabel添加到alertView:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Centered Title" message:@"\n\n\n" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12.0, 24.0, 250.0, 80.0)];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentLeft;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.text = @"Here is an example of a left aligned label in a UIAlertView!";
    [alert addSubview:label];
    [alert show];

參考: 如何僅在UIAlertView中對齊標題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM