2012-04-08 19 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

答えて

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プロパティを使用して簡単に行うことができます。 UIのデバッグチップとして

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

、通常、そのような多くの問題を解決するのに役立ちますどの、それは取っているまさに面積を示すために、問題ビュー(ラベル)の背景を変更してみてください。

+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

+1

@InsertWittyName - チップのおかげで、答えに追加させてください。 – Anurag

+0

最後の2行+1 :-) –

2

backgroundColoraLabelaReflectionViewに設定して、どこに配置されているかを確認してください。

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)]; 

aLabelaReflectionView内に含まれているので、それはaReflectionView境界ではなく、self.viewの範囲内でセンタリングされるべきです。単にaReflectionViewを埋めるaLabelを作るために良いかもしれ、あなたの目的のために

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

:変更を確認するために、右にスクロール

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

を問題はUILabelのテキストの整列ではなく、むしろUILabel自体のアラインメントです。具体的には、self.view(特に幅)の幅に合わせて中央揃えになるようにUILabelを作成していますが、aReflectionView(これは幅が狭く、自己の左端からオフセットされています)に追加しています。表示され、すでに画面の中央に表示されています)。この場合の正味の効果は、自己の左端からオフセットを取得していることです。aReflectionViewのx座標で1回、次にaReflectionViewに配置したaLabelのx座標で再び2回表示します。これは明らかにあなたの意図ではありません。あなたはそれを入れているビュー上のラベルを中央揃え、

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)"; 
関連する問題