如何水平居中UILabel TEXT?

J-L*_*-LO 3 objective-c uilabel ios

现在我已经看到很多例子教你如何将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];
Run Code Online (Sandbox Code Playgroud)

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

Gun*_*nds 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];
Run Code Online (Sandbox Code Playgroud)