【发布时间】:2010-10-18 16:31:12
【问题描述】:
在UIView 中将标签居中的最佳方法是什么?如果您执行诸如
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(view.frame.origin.x / 2, view.frame.origin.y / 2, 50.0, 50.0)];
然后您将标签的原点设置为视图的中心。最好的办法是使用 center 属性将视图的中心设置为该点。所以我尝试使用以下代码:
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)];
这会产生一个标签,该标签几乎超出了左上角的视图范围。
【问题讨论】:
标签: ios objective-c iphone cocoa-touch uilabel