0
それぞれ異なるフォントサイズを持つtow UILabelを整列したいと思います。StoryBoardを使用せずに2つの異なる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が、私は今のところ良い答えを得ることができませんでした。
How to align UILabel's baseline to the bottom of a UIImageView?
あなたは2 UILabelsを揃えるための良い方法を知っていれば、あなたは私の道を教えてくださいだろうか?
ありがとうございます。