2014-06-23 42 views
0

我必须使用两个连续的标签,如:如何以编程方式对齐和移动两个标签?

Title: New Title 

但是标题是,改变不同的语言他长度的本地化字符串。如何以编程方式将New title的UILabel移动到Title的末尾?使用autoLayout时,在StackOverflow上找到的旧解决方案似乎不工作。

+0

显示您设置帧或自动布局限制的代码。 –

+0

检查此[链接](http://stackoverflow.com/questions/13152262/iphone-adjust-uilabel-width-according-to-the-text),可能会有所帮助! – nikhil84

回答

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)]; 
+0

是的,那正是我正在寻找的。 BTW的iOS有点令人毛骨悚然,做这件事情:\ – fustalol

0

在第一帧之后设置第二个标签帧。 label2 x点将为label1 x point + lable1长度。

+0

我知道,但它不起作用。使用自动布局,您不能在'viewDidLoad'中设置框架。 – fustalol

0

你为什么不只是使用一个标签与文本格式化:

mylabel.text = [的NSString stringWithFormat:@ “%@:%@”,标题,新标题]。

+0

因为第一部分是浅蓝色的粗体,第二部分是红色和普通字体。 – fustalol

+0

@fustalol然后使用NSAttributedString,您可以为不同的范围设置不同的颜色。 –

0

尝试LABEL2的领导之间添加NSLayoutConstraints与LABEL1的尾随常数

value = 0; 

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

试试这个:

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

CGRect frame = label1.frame; 

[label2 setFrame:CGRectOffset(frame, frame.size.width, 0)]; 
相关问题