【问题标题】:How do I center UILabel TEXT horizontally?如何水平居中 UILabel TEXT?
【发布时间】:2013-10-22 19:53:56
【问题描述】:

现在我已经看到了很多示例,这些示例教您如何将 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,然后将文本居中,但这不起作用。有什么想法吗?

【问题讨论】:

  • 你可以在界面生成器中构建它吗?您可以使用 NSLayoutContraints 将视图固定到中心;在 IB 中很容易做到。

标签: ios objective-c uilabel


【解决方案1】:

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

【讨论】:

  • 天哪。我不敢相信我错过了!谢谢你,Guntis!
猜你喜欢
  • 1970-01-01
  • 2014-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-11
  • 2012-01-12
相关资源
最近更新 更多