2012-04-08 66 views
1

我試圖水平居中我的UILabel。這就是它最終的樣子(注意它沒有中心)。爲什麼我的UILabel不居中?

enter image description here

ReflectionView *aReflectionView = [[ReflectionView alloc]initWithFrame:CGRectMake(((self.view.bounds.size.width/2) - 150), 40, 300, 90)]; 
    UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(((self.view.bounds.size.width/2) - 150), 0, 300, 90)]; 
    aLabel.textAlignment = UITextAlignmentCenter; 
    aLabel.text = @"1 lbs"; 
    self.weightLabel = aLabel; 
    [aReflectionView addSubview:self.weightLabel]; 
    self.weightReflectionView = aReflectionView; 
    [self.view addSubview:self.weightReflectionView ]; 
    [self.weightReflectionView updateReflection]; 
+0

看起來像文字太大。收縮它,看看它是否得到中心確認。 – wbyoung 2012-04-08 01:00:07

回答

7

textAlignment屬性將所述UILabel的框架內對齊文本,而不是外部的它。我的ASCII技能有點生疏,但這就是我的意思。考慮下面的標籤,其中|表示標籤的左/右邊緣。

 
|173 lbs | => left aligned 
| 173 lbs| => right aligned 
| 173 lbs | => center aligned (not exactly centered in terms of its superview) 

現在考慮包含在另一視圖,其中外[]代表含視圖的邊緣內相同的標記,並且內|表示包含標籤的邊緣。注意內部標籤如何不在其超視圖中精確居中,並稍微向右推(距左側2個空間,靠近右側1個空間)。

 
[ |173 lbs | ] => left aligned 
[ | 173 lbs| ] => right aligned 
[ | 173 lbs | ] => center aligned 

如果您希望標籤爲中心,在任何時候上海華或屏幕內對齊,你會想,以確保標籤的寬度它的容器相匹配,然後設置textAlignment到中心。操作方法如下:

 
[|173 lbs  |] => left aligned 
[|  173 lbs|] => right aligned 
[| 173 lbs |] => center aligned (perfectly aligned) 

這是您希望子視圖與超視圖的確切大小相匹配的常見情況。你可以使用bounds屬性來輕鬆做到這一點。

ReflectionView *aReflectionView = [[ReflectionView alloc] initWithFrame:..]; 
// Use the bounds of the superview to set the subview's frame. 
UILabel *aLabel = [[UILabel alloc] initWithFrame:aReflectionView.bounds]; 

作爲UI調試尖,嘗試改變問題的視圖(標籤)的背景顯示什麼區域正在採取,這通常有助於解決很多這樣的問題。

+0

很好解釋。基本上改變UILabel * aLabel = [[UILabel alloc] initWithFrame:CGRectMake(((self.view.bounds.size.width/2) - 150),0,300,90)];到UILabel * aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,300,90)]; – InsertWittyName 2012-04-08 01:15:23

+1

@InsertWittyName - 感謝您的提示,讓我把它添加到答案。 – Anurag 2012-04-08 01:16:37

+0

最後兩行+1 :-) – 2013-09-20 11:47:21

2

僅用於調試目的,請嘗試設置aLabelaReflectionViewbackgroundColor以查看它們的放置位置。

aLabel.backgroundColor = [UIColor redColor]; 
aReflectionView.backgroundColor = [UIColor blueColor]; 

你應該看到aReflectionView正確它的父內居中,但aLabel,其中包含的,是不是。爲什麼不?由於該行的:

UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(((self.view.bounds.size.width/2) - 150), 0, 300, 90)]; 

由於aLabel包含內aReflectionView,它應在aReflectionView範圍內居中,的self.view不邊界。向右滾動才能看到的變化:

UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(((aReflectionView.bounds.size.width/2) - 150), 0, 300, 90)]; 

你的目的,它可能是更明智aLabel只需填寫aReflectionView

UILabel *aLabel = [[UILabel alloc]initWithFrame:aReflectionView.bounds]; 
0

的問題是不是在的UILabel的文本的對齊方式,但而不是UILabel本身的對齊。具體來說,你創建的UILabel要根據self.view的寬度(特別是寬度)居中對齊,但是你將它添加到aReflectionView(它比自己的左邊緣偏離更窄)。視圖,並且已經在屏幕上居中)。在這種情況下的淨效應是,你得到了自我左邊緣的偏移量。查看兩次,一次在aReflectionView的x座標中,然後再次放入aReablectionView中的aLabel的x座標中,這顯然不是您的意圖。最簡單的解決方案可能是這樣的:

UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(((aReflectionView.bounds.size.width/2) - 150), 0, 300, 90)]; 

底線,將標籤放在您要放置的視圖上。

0

對於那些谷歌搜索,我有一個答案。問題是,我有一些轉義字符的代碼內指定一個新行,像這樣:

self.emptyLabel.text = @"No likes yet! \n\ 
\ 
\n(Like some cards to save them here)"; 

導致文本偏離中心對齊。只需將它們移除,如下所示:

self.emptyLabel.text = @"No likes yet! \n\n(Like some cards to save them here)";