看起来简单的东西,但我不能让它工作,我想要的方式, 这里还有不少类似的问题,但他们不解决我的问题。动态中心和调整的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子视图居中的视图?
感谢
您需要使用autolayout的解决方案? – YaBoiSandeep 2015-02-11 06:21:23
我需要一个可以通过编程来实现(无情节串连图板或xibs)的解决方案,如果我可以使用自动布局这种方式则是 – 2015-02-11 08:19:58