【问题标题】:how to programmatically align a label to an x, y coordinate如何以编程方式将标签与 x、y 坐标对齐
【发布时间】:2014-11-27 23:58:36
【问题描述】:

下面是标签的一些其他属性。显然 nstextalignmentleft 不是我想要的。无法理解在哪里输入坐标。

self.lblTimer = [[UILabel alloc] init];
self.lblTimer.translatesAutoresizingMaskIntoConstraints = NO;
self.lblTimer.textAlignment = NSTextAlignmentLeft;
self.lblTimer.font = [UIFont systemFontOfSize:10];
self.lblTimer.textColor = [UIColor redColor];`self.lblTimer = [[UILabel alloc] init];

【问题讨论】:

  • 你想做什么? textAlignment 只能设置为左、右、居中、对齐和自然。您是否要在标签边缘和文本之间创建偏移量?
  • 标签是一个倒计时的计时器。我只是想把它放在屏幕的左下角。

标签: ios alignment label coordinates


【解决方案1】:

我认为您误解了 textAlignment 的作用。这只是控制标签内的对齐方式,而不是屏幕上的对齐方式。如果要在屏幕上放置标签,则必须更改其frame 属性:

self.lblTimer.frame = CGRectMake(x,y,width,height); 

【讨论】:

    【解决方案2】:

    由于您已将self.lblTimer.translatesAutoresizingMaskIntoConstraints 设置为NO,我猜您想使用Auto Layout,如果是这样,我强烈建议您查看Masonry

    另一种方法是在标签上设置 frame 属性,需要 CGRect 在其中给它 x 和 y 坐标以及宽度和高度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多