0
각각 다른 글꼴 크기를 가진 tow UILabel을 정렬하고 싶습니다.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];
}
나는 다음과 같은 질문에 다른 사람을 reffered 그러나 나는 지금까지 좋은 답변을 얻을 수 없었다. 두 개의 UILabels를 정렬 할 수있는 좋은 방법을 알고 있다면
How to align UILabel's baseline to the bottom of a UIImageView?
, 당신은 나에게 길을 알려주십시오 수 있을까?
감사합니다.