iphone - 在 iOS 上设计 3 行对话框的方法 - 多语言可用

标签 iphone ios cocoa-touch interface-builder autolayout

我正在尝试在 iOS 中创建一个 View (普通的单一 View ),其中包含 3 个标签和 3 个文本字段。

该应用程序应该能够支持多语言使用。

我想要以下布局:

Label | TextField
Label | TextField
Label | TextField

文本字段应从相同位置开始。 (见图)

但是文本字段的长度应取决于标签中文本的长度。 (我想最小化您在英语图像上看到的空间。

简而言之,英语布局的标签和文本字段之间的间距应与德语布局相同。

使用自动布局来执行此操作的最佳方法是什么?

Image (english), to many space Image (german)

最佳答案

每行首先: 调整标签大小以适合文本。 然后在标签和文本字段之间添加水平间距。

然后在文本字段上添加更高优先级的约束,使所有左边缘都应对齐。

这是一些针对两行执行此操作的代码:

- (void)viewDidLoad{
      [super viewDidLoad];

      //create labels and text fields for row 1
      UILabel *label0,*label1;
      UITextField *textField0,*textField1;

      label0 = [[UILabel alloc] init];
      label0.backgroundColor=[UIColor redColor];
      [self.view addSubview:label0];


      textField0 = [[UITextField alloc] init];
      textField0.backgroundColor=[UIColor greenColor];
      [self.view addSubview:textField0];

      [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
      [label0 setTranslatesAutoresizingMaskIntoConstraints:NO];
      [textField0 setTranslatesAutoresizingMaskIntoConstraints:NO];

      //*************************************************************
      //layout row 1
      NSArray *arr;
      NSDictionary *dict = NSDictionaryOfVariableBindings(label0,textField0);
      NSLayoutConstraint *constraint;

      //label sized to fit text (default) and standard spacing between label and textField
      arr = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[label0]-[textField0(100)]"
                                                    options:0
                                                    metrics:nil
                                                      views:dict];
      [self.view addConstraints:arr];

      //vertical constraints
      arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label0]"
                                                    options:0
                                                    metrics:nil
                                                      views:dict];
      [self.view addConstraints:arr];

      arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[textField0]"
                                                    options:0
                                                    metrics:nil
                                                      views:dict];
      [self.view addConstraints:arr];


      //*************************************************************
      //create label and textfield for row2
      label1 = [[UILabel alloc] init];
      label1.backgroundColor=[UIColor redColor];
      [self.view addSubview:label1];

      textField1 = [[UITextField alloc] init];
      textField1.backgroundColor=[UIColor greenColor];
      [self.view addSubview:textField1];

      [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
      [label1 setTranslatesAutoresizingMaskIntoConstraints:NO];
      [textField1 setTranslatesAutoresizingMaskIntoConstraints:NO];

      //*************************************************************
      //layout row 2
      dict = NSDictionaryOfVariableBindings(label0,textField0,label1,textField1);

      //label sized to fit text (default) and standard spacing between label and textField
      arr = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[label1]-[textField1(100)]"
                                                    options:0
                                                    metrics:nil
                                                      views:dict];
      [self.view addConstraints:arr];

      //vertical constraints
      arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label0]-[label1]"
                                                    options:0
                                                    metrics:nil
                                                      views:dict];
      [self.view addConstraints:arr];

      arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[textField0]-[textField1]"
                                                    options:0
                                                    metrics:nil
                                                      views:dict];
      [self.view addConstraints:arr];



      //*************************************************************
      //line up the left edges of the text fields and make it a higher priority to override spacing
      constraint = [NSLayoutConstraint constraintWithItem:textField0 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:textField1 attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
      constraint.priority=UILayoutPriorityDefaultHigh;
      [self.view addConstraint:constraint];

      //*************************************************************
      //Setup some text  
      //label0.text=@"Label0";
      label0.text=@"Some long textasdfasdfadsfasfdasf";
      textField0.text = @"textField0";

      //label1.text=@"Some long text";
      label1.text=@"Label1";
      textField1.text = @"textField1";

      [self.view setNeedsLayout];
}

关于iphone - 在 iOS 上设计 3 行对话框的方法 - 多语言可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14842585/

相关文章:

ios 图片文字和颜色增强滤镜

ios - 当应用程序进入前台时重新启动应用程序

ios - gboard 应用程序如何在 iOS 中运行?

objective-c - iOS: ZBar SDK unicode字符

ios - iPad - 从前置摄像头拍照并识别驾照

javascript - 如何从 jQuery 代码中获取坐标值 (x,y) 并存储在 NSString 中?

cocoa - Interface Builder 不显示我的一些资源图像

iphone - 在忽略文件的同时将 App 转换为 ARC?

ios - iPad 的条形码生成器代码或 SDK

iPhone UDP广播和响应