2014-06-23 84 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)]; 
相關問題