2016-11-26 7 views
0

それぞれ異なるフォントサイズを持つtow UILabelを整列したいと思います。StoryBoardを使用せずに2つの異なる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]; 
} 

は、私は、次の質問などをrefferedが、私は今のところ良い答えを得ることができませんでした。

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

あなたは2 UILabelsを揃えるための良い方法を知っていれば、あなたは私の道を教えてくださいだろうか?

ありがとうございます。

答えて

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

関連する問題