如何水平居中 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,然后仅将文本居中,但这不起作用。有什么想法吗?

你应该正确设置框架: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];

最新更新