相关疑难解决方法(0)

在UIView中居中标签

将标签置于中心的最佳方法是UIView什么?如果你做的事情如

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

然后,您将标签的原点设置为视图的中心.最好的选择是使用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)];
Run Code Online (Sandbox Code Playgroud)

这会产生一个标签,它几乎超出了左上角视图的范围.

iphone cocoa-touch objective-c uilabel ios

8
推荐指数
2
解决办法
1万
查看次数

标签 统计

cocoa-touch ×1

ios ×1

iphone ×1

objective-c ×1

uilabel ×1