簡體   English   中英

如何在不使用StoryBoard的情況下對齊兩個不同UILabel的基線

[英]How to align baseline of two different UILabel without using StoryBoard

我想對齊兩個具有不同字體大小的UILabel。

我正在嘗試不使用StoryBoard來執行此操作,但是我不能。

在屏幕截圖中,兩個UILabel的基線不同。

在此處輸入圖片說明

我的代碼如下

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

我回答了以下問題和其他問題,但到目前為止我還沒有很好的答案。

如何將UILabel的基線與UIImageView的底部對齊?

如果您知道對齊兩個UILabel的好方法,請告訴我方法嗎?

謝謝。

我自己解決了這個問題,沒有使用約束。

我發布固定代碼和屏幕截圖。

謝謝。

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM