在 UIView 中將標籤居中 (Centering a label in a UIView)


問題描述

在 UIView 中將標籤居中 (Centering a label in a UIView)

What's the best way to center a label in a UIView? If you do something such as

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(view.frame.origin.x / 2, view.frame.origin.y / 2, 50.0, 50.0)];

Then you're setting the origin point of the label to the center of the view. The best bet would be to set the center of the view to that point using the center property. So I tried using the following code:

UIView *aView = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
aView.backgroundColor = [UIColor darkGrayColor];
CGRect frame = aView.frame;

UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 125.0f, 30.0f)];
[aLabel setCenter:CGPointMake(frame.origin.x / 2, frame.origin.y / 2)];

That yields a label which is pretty much outside the bounds of the view in the upper left hand corner.


參考解法

方法 1:

The main thing you are doing wrong is taking half the origin values, rather than half the sizes

However, you don't need to even calculate that in this case - just do something like the following:


UIView *aView = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
aView.backgroundColor = [UIColor darkGrayColor];

UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 125, 30)];
aLabel.center = aView.center;

(note, you don't need to force those coordinates to floats - in this case writing them as ints seems more readable).

Also, it's a matter of style - but since you're already using property syntax (aView.backgroundColor) you may as well use it for the center property too :-)

方法 2:

To position any child horizontally centered in a parent you would calculate its position like so;

childX = (parentWidth - childWidth) / 2

(This also applies to height).

(by Coocoo4CocoaphilsquaredAndrew Grant)

參考文件

  1. Centering a label in a UIView (CC BY-SA 3.0/4.0)

#cocoa-touch #uilabel #objective-c #iphone #iOS






相關問題

防止 UITableView 在自定義子視圖處理觸摸時滾動 (Prevent UITableView from scrolling when custom subview is handling touches)

NSMutableArray removeObjectAtIndex 在第二次執行時崩潰 (NSMutableArray removeObjectAtIndex crashes on second execution)

Apakah NSURLConnections termasuk dalam model atau pengontrol saya? (Do NSURLConnections belong in my model or controller?)

Lợi ích của việc sử dụng NSString tĩnh cho CellIdentifier là gì? (Whats the advantage of using a static NSString for CellIdentifier?)

CGContextRef 中的坐標係是什麼樣的? (How does the coordinate system in an CGContextRef look like?)

無法在不崩潰的情況下設置 ABPeoplePickerNavigationController 的 addressBook 屬性 (Can't set the addressBook property of ABPeoplePickerNavigationController without crashing)

tableViewHeader 拒絕多個子視圖 (tableViewHeader rejecting multiple sub-views)

界面生成器添加了一個標籤欄按鈕,但未顯示 (Interface builder added one tab bar button, but not showing up)

iVar getter / 方法同名? (iVar getter / method with same name?)

如何更改主視圖是基於導航的應用程序! (how to change main view is navigation based app!)

捕捉返回按鈕導航事件 (Catching back button navigation event)

如何將對象轉發到單獨的子視圖? (How can i forward objects to separate subviews?)







留言討論