【问题标题】:Vertical Shift UILabel Text Without Affecting Background在不影响背景的情况下垂直移动 UILabel 文本
【发布时间】:2014-03-28 09:46:27
【问题描述】:

我一直在试图弄清楚如何将我的 UILabel 文本垂直对齐到顶部或底部。问题是,我后面有一个图像,它也随着文本移动。

有没有办法将背景颜色/图像保持在原位,但只能向上或向下移动文本?

这是当前代码:

- (id)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (!self) return nil;

  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,
                                                             0,
                                                             frame.size.width,
                                                             frame.size.height)];
  label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont fontWithName:@"AvenirNext-Bold" size:12];

  label.backgroundColor = [UIColor clearColor];

  [self addSubview:label];
  self.numberLabel = label;
  self.layer.cornerRadius = 3;
  return self;
}

我一直在关注本教程,但它改变了一切,包括我的标签背景。 Vertically align text to top within a UILabel

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    找到文本的高度。 使用另一个视图显示图像。 使用文本的高度和要为标签设置的任何位置设置标签相对图像视图的框架。

    【讨论】:

      【解决方案2】:

      如果您不使用自动布局,请在标签上使用:

      [YOUR_LABEL sizeToFit];
      

      这将调整标签的大小以适合文本。

      什么是背景图片?一个单独的UIImageView ?您如何设置此图像?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-23
        • 1970-01-01
        • 2014-05-16
        • 1970-01-01
        • 2017-08-27
        • 2014-11-23
        相关资源
        最近更新 更多