【问题标题】:How to change message's aligment in UIAlertView?如何更改 UIAlertView 中的消息对齐方式?
【发布时间】:2015-11-12 06:25:26
【问题描述】:

  当我使用 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];

效果如下:
如我们所见,文本“这是第一行”在中心,但我想将其更改为左侧。我知道 UILabel 可以更改文本的对齐方式,但我不知道是否可以在 UIAlertView 中更改它。

【问题讨论】:

    标签: ios css uialertview


    【解决方案1】:

    像这样编辑你的代码。

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

    【讨论】:

    • 是的,它有效。但我想知道密钥“accessoryView”。有没有文件提到它?另外,设置标签“v”的框架不起作用。
    • 但它在NSAlert中工作,在mac开发中很有用。 UIAlertView 一样吗?
    • "accessoryView" 是私有的。所以,这可能你的应用被拒绝了。所以,我建议你使用 UIAlertViewController / 自定义你的警报。
    • 哦,你的意思是“附件视图”没有在.h文件中声明?所以它是私人的。但是如果在.h文件中声明了但没有声明为属性,那也是私有的吗?
    • 另外,如何在一个类中找到私有变量或私有方法。我很感兴趣。
    【解决方案2】:

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

    参考:How to make a multiple line, left-aligned UIAlertView?

    或者你可以在alertView中添加一个UILabel:

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

    参考:How to align only the title in UIAlertView

    【讨论】:

    • 我尝试使用这个解决方案。但是我发现 subViewArray 里面没有对象。
    • @huixing 是的,它只适用于 iOS
    猜你喜欢
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多