0

I have to use two sequential label, like:

Title: New Title

But title is a localized string that change his length in different languages. How to programmatically move the UILabel of New title at the end of Title ? With autoLayout the old solutions found on StackOverflow don't seem to be working.

Jeremy D
  • 4,787
  • 1
  • 31
  • 38
Alessio Crestani
  • 1,602
  • 4
  • 17
  • 38
  • Show the code where you set whether the frame or the auto layout constraints. – The dude Jun 23 '14 at 07:54
  • Check this [link](http://stackoverflow.com/questions/13152262/iphone-adjust-uilabel-width-according-to-the-text) out ,might be helpful ! – nikhil84 Jun 23 '14 at 08:32

5 Answers5

1

Use one string

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

If color of title and text are different use attributed string like this example -

   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)];
Yogendra
  • 1,728
  • 16
  • 28
0

set the second label frame after the first frame. label2 x point will be label1 x point + lable1 length.

pqteru
  • 451
  • 1
  • 7
  • 23
0

Why don't you use just one label with text is formated:

mylabel.text = [NSString stringWithFormat:@"%@: %@", Title, newtitle];

meaholik
  • 440
  • 5
  • 19
0

try to add NSLayoutConstraints between the leading of label2 and trailing of label1 with the constant

value = 0;

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:label2
                                                  attribute:NSLayoutAttributeLeading
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:label1
                                                  attribute:NSLayoutAttributeTrailing
                                                 multiplier:1
                                                   constant:0]];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
meaholik
  • 440
  • 5
  • 19
0

Try this as well:

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

CGRect frame = label1.frame;

[label2 setFrame:CGRectOffset(frame, frame.size.width, 0)];
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56