【问题标题】:How to align messages in UIAlertView?如何在 UIAlertView 中对齐消息?
【发布时间】:2009-09-08 06:49:32
【问题描述】:

我想知道如何设置警报视图的委托消息的对齐方式。任何人都有解决方案,请回复一些代码。

【问题讨论】:

    标签: iphone objective-c xcode


    【解决方案1】:

    这只是上一个答案的略微简化版本,但我喜欢保持简单。 :)

    for (UIView *view in alert.subviews) {
        if([[view class] isSubclassOfClass:[UILabel class]]) {
            ((UILabel*)view).textAlignment = NSTextAlignmentLeft;
        }
    }
    

    【讨论】:

    • UITextAlignmentLeft 已弃用,请使用 NSTextAlignmentLeft
    【解决方案2】:

    你需要获取alertView的子视图。遍历子视图的数组,它将有一个 UILable 类型的项目。从子视图数组中获取 UILabel 并为此设置 textAlignment 属性。

    NSArray *subViewArray = alertView.subviews;
     for(int x=0;x<[subViewArray count];x++){
     if([[[subViewArray objectAtIndex:x] class] isSubclassOfClass:[UILabel class]])
      {
          UILabel *label = [subViewArray objectAtIndex:x];
        label.textAlignment = UITextAlignmentCenter;
      }
    
    }
    

    【讨论】:

    • 有效。谢谢。不过有一个小错字:第一行的“subViews”应该是“subview” :-)
    • 它实际上应该是“子视图”! :D
    【解决方案3】:

    下面的代码不适用于iOS7,只能在iOS7之前使用。

    for (UIView *view in alert.subviews) {
        if([[view class] isSubclassOfClass:[UILabel class]]) {
           ((UILabel*)view).textAlignment = NSTextAlignmentLeft;
        }
    }
    

    问题Align message in UIAlertView to left in iOS 7解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多