如何水平居中UILabel TEXT?

时间:2013-10-22 19:53:56

标签: ios objective-c uilabel

现在我已经看到很多例子教你如何将UILabel文本置于屏幕中间,但这不是我要求的。我问的是如何确保UILabel的文本保持在屏幕左右边界之间的中间位置,而我所要做的就是垂直进行调整。我有以下内容:

self.aboutMee = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, 120, self.view.bounds.size.width)];
            self.aboutMee.text = @"About Me";
            self.aboutMee.textAlignment = NSTextAlignmentCenter;
            self.aboutMee.backgroundColor = [UIColor clearColor];
            self.aboutMee.font = [UIFont fontWithName:@"Helvetica" size:14];
            [self.aboutMee setTextColor:[UIColor colorWithRed:0.0/255.0 green:140.0/255.0 blue:199.0/255.0 alpha:1.0]];
            [self.aboutMee setFont:[UIFont fontWithName:@"HelveticaNeueLTStd-ThCn" size:35]];
            [self.scrollView addSubview:self.aboutMee];

现在我尝试将整个标签的宽度设为320,然后将文本居中,但这不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:10)

你应该正确设置框架:CGRectMake(pos.x,pos.y,width,height)

self.aboutMee = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        self.aboutMee.text = @"About Me";
        self.aboutMee.textAlignment = NSTextAlignmentCenter;
        self.aboutMee.backgroundColor = [UIColor clearColor];
        self.aboutMee.font = [UIFont fontWithName:@"Helvetica" size:14];
        [self.aboutMee setTextColor:[UIColor colorWithRed:0.0/255.0 green:140.0/255.0 blue:199.0/255.0 alpha:1.0]];
        [self.aboutMee setFont:[UIFont fontWithName:@"HelveticaNeueLTStd-ThCn" size:35]];
        [self.scrollView addSubview:self.aboutMee];