如何在ios 6中定位UILabel?

时间:2013-04-09 17:05:59

标签: iphone ios

我认为2个标签,较大的标签和较小的标签只是位于屏幕的中心。 更改坐标似乎不会对它们放置的位置产生影响。为什么呢?

理想情况下,我希望它们具有相同的尺寸,不重叠,一个在另一个之下,以便它们几乎覆盖整个屏幕。

我认为labelframe使用的格式 - 起始坐标,大小。 我的起源在哪里?在屏幕的左上角? 我可以在xcode中翻译吗?

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UILabel *label1;

@end

@implementation ViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    CGRect labelFrame = CGRectMake(-200.0f,0.0f, 200.0f, 200.0f);
    self.label = [[UILabel alloc] initWithFrame:labelFrame];
    //self.label.adjustsFontSizeToFitWidth = YES;
    self.label.text = @"One two three four five six seven eight nine.";
    self.label.font = [UIFont boldSystemFontOfSize:28.0f];
    self.label.numberOfLines  = 5;
    self.label.center = self.view.center;
    [self.view addSubview:self.label];

    CGRect labelFrame1 = CGRectMake(600.0f,600.0f, 100.0f, 100.0f);
    self.label1 = [[UILabel alloc] initWithFrame:labelFrame1];
    self.label1.text = @"One 1 two three four five six seven eight nine.";
    self.label1.font = [UIFont boldSystemFontOfSize:18.0f];
    self.label1.numberOfLines  = 0;
    self.label1.center = self.view.center;
    [self.view addSubview:self.label1];



}

@end

2 个答案:

答案 0 :(得分:0)

您正在覆盖拨打电话时设置的来源

self.label.center = self.view.center

答案 1 :(得分:0)

Origin 0,0是屏幕 superview的左上角。如果您希望标签位于屏幕中央,一个在另一个上面,您可以执行以下操作:

self.label = [[UILabel alloc] init];
// setup label text, font..
[self.label sizeToFit]; // automatically set the appropriate size
self.label.center = self.view.center;
[self.view addSubview:self.label];

self.label1 = [[UILabel alloc] init];
// setup label text, font..
[self.label sizeToFit]; // automatically set the appropriate size
self.label1.center = self.view.center; // set at center of screen
// just overwrite origin.y to set it below the previous label
self.label1.frame = (CGRect) { self.label1.frame.origin.x, self.label.frame.origin.x + self.label.frame.size.height, self.label1.frame.size };
[self.view addSubview:self.label1];

更新:根据您的评论,让它有两行,一列更容易。只需将框架设置为全宽和半高:

self.label = [[UILabel alloc] initWithFrame:CGrectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2];
self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2];