2016-11-26 4 views
0

각각 다른 글꼴 크기를 가진 tow UILabel을 정렬하고 싶습니다.StoryBoard를 사용하지 않고 두 개의 다른 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 그러나 나는 지금까지 좋은 답변을 얻을 수 없었다. 두 개의 UILabels를 정렬 할 수있는 좋은 방법을 알고 있다면

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

, 당신은 나에게 길을 알려주십시오 수 있을까?

감사합니다.

답변

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