2015-02-10 72 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