看起來簡單的東西,但我不能讓它工作,我想要的方式, 這裏還有不少類似的問題,但他們不解決我的問題。動態中心和調整的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