2015-02-10 81 views
0

看起來簡單的東西,但我不能讓它工作,我想要的方式, 這裏還有不少類似的問題,但他們不解決我的問題。動態中心和調整的UILabel寬度編程

我使用以下代碼來設置中心在它的父

具有兩個UILabels作爲子視圖,點變化也數的圖。

NSString *myScoreText = [NSString stringWithFormat:@"My Score: "]; 
UIFont *myScoreFont = [UIFont boldSystemFontOfSize:14.0f]; 
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:myScoreFont, NSFontAttributeName, nil]; 
CGFloat myScoreWidth = [myScoreText sizeWithAttributes:attributes].width; 

NSString *pointsText = [NSString stringWithFormat:@"%d points", currentScore]; 
UIFont *pointsFont = [UIFont boldSystemFontOfSize:14.0f]; 
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:pointsFont, NSFontAttributeName, nil]; 
CGFloat pointsWidth = [pointsText sizeWithAttributes:attributes].width; 

CGFloat scoreViewWidth = myScoreWidth + pointsWidth; 
UIView *scoreView = [[UIView alloc] initWithFrame:CGRectMake(FRAME_WIDTH/2 - scoreViewWidth/2, elementHeight, scoreViewWidth, LABEL_HEIGHT)]; 

UILabel *myScoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myScoreWidth, LABEL_HEIGHT)]; 
myScoreLabel.font = myScoreFont; 
myScoreLabel.text = myScoreText; 
myScoreLabel.textAlignment = NSTextAlignmentLeft; 

self.pointsLabel = [[UILabel alloc] initWithFrame:CGRectMake(myScoreWidth, 0, pointsWidth, LABEL_HEIGHT)]; 
self.pointsLabel.textColor = [UIColor redColor]; 
self.pointsLabel.font = pointsFont; 
self.pointsLabel.text = pointsText; 
self.pointsLabel.textAlignment = NSTextAlignmentLeft; 

[scoreView addSubview:myScoreLabel]; 
[scoreView addSubview:self.pointsLabel]; 

後的視圖顯示在點是10-99之間,但如果分提高到100分能正常工作的視圖顯示像文本:My Score: 100 po...代替My Score: 100 points當得分超過1000,則查看顯示My Score: 1000 p...等等......

所以我怎樣才能讓所有的文本正確顯示,並仍然保持其兩個UILabels子視圖居中的視圖?

感謝

+0

您需要使用autolayout的解決方案? – YaBoiSandeep 2015-02-11 06:21:23

+0

我需要一個可以通過編程來實現(無情節串連圖板或xibs)的解決方案,如果我可以使用自動佈局這種方式則是 – 2015-02-11 08:19:58

回答

0

隨着自動佈局:

UILabel *label = [[UILabel alloc]initForAutoLayout]; 

label.translatesAutoresizingMaskIntoConstraints= NO; 


[self.view addSubview:label]; 

[self.view addConstraint: [NSLayoutConstraint 
              constraintWithItem:label attribute:NSLayoutAttributeCenterX 
              relatedBy:NSLayoutRelationEqual toItem:self.view attribute: 
              NSLayoutAttributeCenterX multiplier:1.0 constant:0.0f]]; 







[self.view addConstraint:[NSLayoutConstraint constraintWithItem:label 
    attribute:NSLayoutAttributeTop 
    relatedBy:NSLayoutRelationEqual 
toItem:self.view 
    attribute:NSLayoutAttributeTop  
    multiplier:1.0  
    constant:100.0]]; 

label.text = @"a"; 

隨着Purelayout:

添加以下PureLayout庫

https://github.com/smileyborg/PureLayout

,因爲它是比較容易處理的自動佈局使用Purelayout 。

UILabel *label = [[UILabel alloc]initForAutoLayout]; 

[scoreView addSubview:label]; 

[label autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0.0f]; 

[label autoAlignAxisToSuperviewAxis:ALAxisVertical]; 

[label setText:@"hello"]; 

通過使用ALAxisVertical使文本居中,也可以使用ALAxisHorizo​​ntal水平居中。

+0

謝謝您的時間和幫助,但我一直在尋找無外庫的解決方案 – 2015-02-11 11:36:28

+0

檢查更新的答案與自動佈局 – YaBoiSandeep 2015-02-12 10:22:48