如何在不使用StoryBoard的情况下对齐两个不同UILabel的基线

时间:2016-11-26 18:26:30

标签: ios iphone alignment uilabel vertical-alignment

我想对齐每个字体大小不同的UILabel。

我试图在不使用StoryBoard的情况下这样做,但我不能。

在屏幕截图中,两个UILabel的基线不同。

enter image description here

我的代码如下,

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self initLabel1];
    [self initLabel2];

    [self addSideConstraint];
    [self addBaseLineConstraint];
}

- (void)initLabel1
{
    _label1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 120, 50)];
    _label1.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_label1];

    _label1.font = [UIFont systemFontOfSize:18];
    _label1.textAlignment = NSTextAlignmentRight;
    _label1.text = @"abcjkg";
}

- (void)initLabel2
{
    _label2 = [[UILabel alloc] init];
    _label2.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_label2];

    _label2.font = [UIFont systemFontOfSize:36];
    _label2.translatesAutoresizingMaskIntoConstraints = NO;
    _label2.text = @"abcjkg";

}

- (void)addSideConstraint
{
    NSLayoutConstraint *layoutConstraint2 = [NSLayoutConstraint
                                             constraintWithItem:self.label2
                                             attribute:NSLayoutAttributeLeft
                                             relatedBy:NSLayoutRelationEqual
                                             toItem:self.label1
                                             attribute:NSLayoutAttributeRight
                                             multiplier:1.0f
                                             constant:0];
    [self.view addConstraint:layoutConstraint2];
}

- (void)addBaseLineConstraint
{
    NSLayoutConstraint *layoutConstraint = [NSLayoutConstraint
                                            constraintWithItem:self.label2
                                            attribute:NSLayoutAttributeBaseline
                                            relatedBy:NSLayoutRelationEqual
                                            toItem:self.label1
                                            attribute:NSLayoutAttributeBaseline
                                            multiplier:1.0f
                                            constant:0];
    [self.view addConstraint:layoutConstraint];
}

我提到了以下问题和其他问题但到目前为止我无法得到好的答案。

How to align UILabel's baseline to the bottom of a UIImageView?

如果您知道对齐两个UILabel的好方法,请告诉我这个方法吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题而没有使用约束。

我发布了固定代码和截图。

谢谢。

    - (void)viewDidLoad
{
    [super viewDidLoad];

    [self initLabel1];
    [self initLabel2];
}

- (void)initLabel1
{
    _label1 = [[UILabel alloc] init];
    _label1.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_label1];

    _label1.font = [UIFont systemFontOfSize:18];
    _label1.textAlignment = NSTextAlignmentRight;
    _label1.text = @"abcjkg";
}

- (void)initLabel2
{
    _label2 = [[UILabel alloc] init];
    _label2.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_label2];

    _label2.font = [UIFont systemFontOfSize:36];
    _label2.translatesAutoresizingMaskIntoConstraints = NO;
    _label2.text = @"abcjkg";
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    [self layoutLabel1];
    [self layoutLabel2];
}

- (void)layoutLabel1
{
    CGSize selfSize = self.view.frame.size;
    CGSize fitSize = [_label1 sizeThatFits:selfSize];
    _label1.frame = CGRectMake(50, 50, fitSize.width, fitSize.height);
}

- (void)layoutLabel2
{
    CGSize selfSize = self.view.frame.size;
    CGSize fitSize = [_label2 sizeThatFits:selfSize];

    CGFloat x = CGRectGetMaxX(_label1.frame);
    CGFloat y = CGRectGetMaxY(_label1.frame) - fitSize.height + _label1.font.descender - _label2.font.descender;
    CGFloat width = fitSize.width;
    CGFloat height = fitSize.height;

    _label2.frame = CGRectMake(x, y, width, height);
}

enter image description here