如何以编程方式对齐和移动两个标签?

时间:2014-06-23 07:44:34

标签: ios iphone uilabel

我必须使用两个顺序标签,例如:

Title: New Title

但title是一个本地化的字符串,可以用不同的语言改变他的长度。如何以编程方式在New title末尾移动Title的UILabel?使用autoLayout,StackOverflow上的旧解决方案似乎无法正常工作。

5 个答案:

答案 0 :(得分:1)

使用一个字符串

  NSString *str = [NSString stringWithFormat:@"Title:%@   Newtitle:%@", Title, newtitle];

如果标题和文字的颜色不同,请使用此示例中的属性字符串 -

   NSMutableAttributedString *str = [[NSMutableAttributedString alloc]  initWithString:@"Its an test attributed string."];
    [str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor]       range:NSMakeRange(3,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor]      range:NSMakeRange(10,7)];
  [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)];

答案 1 :(得分:0)

在第一帧之后设置第二个标签帧。 label2 x point将是label1 x point + lable1 length。

答案 2 :(得分:0)

为什么不使用只有一个标签格式的标签:

label.text = [NSString stringWithFormat:@“%@:%@”,标题,新标题];

答案 3 :(得分:0)

尝试在label2的前导和label1的尾随之间添加NSLayoutConstraints,并使用常量

value = 0;

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:label2
                                                  attribute:NSLayoutAttributeLeading
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:label1
                                                  attribute:NSLayoutAttributeTrailing
                                                 multiplier:1
                                                   constant:0]];

答案 4 :(得分:0)

试试这个:

[label1 setText:@"I am label1"];
[label2 setText:@"I am label2"];

CGRect frame = label1.frame;

[label2 setFrame:CGRectOffset(frame, frame.size.width, 0)];