0

I've been trying to figure out how to vertically align my UILabel text to the top or bottom. The problem is, I have an image behind it which also gets shifted with the text.

Is there a way to keep the background color/image in place, but only shift the text up or down?

Here's the current code:

- (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;
}

I've been following this tutorial, but it shifts everything, including my label background. Vertically align text to top within a UILabel

Community
  • 1
  • 1
Taylor
  • 29
  • 5

2 Answers2

0

find the height of the text. Use another view to show image. Set frame of label relative image view with height of the text and whatever position you want to set for label.

saurabh
  • 530
  • 1
  • 8
  • 22
0

ON the label if you're not using auto layout use:

[YOUR_LABEL sizeToFit];

This will resize your label to fit the text.

What is the background image ? A separate UIImageView ? How are you setting this image ?

CW0007007
  • 5,681
  • 4
  • 26
  • 31