垂直对齐两个不同字体大小的标签
Align two labels vertically with different font size
我有一个父对象 UIView
,我在其中放置了两个标签。这些标签中的每一个都只有一行,如下所示:
现在的问题是基线不对。我正在使用自动布局,问题是在这种情况下我的约束应该是什么样子?特别是标签的垂直定位。这些是我目前的限制条件:
H:|-[title]-2-[description]-(>=5)-| //with NSLayoutFormatOptions.AlignAllFirstBaseline
V:|[title]|
V:|[description]|
以上约束导致
Unable to simultaneously satisfy constraints.
因为居中约束和第一个基线约束相互冲突。标签应占据父级的全高,但字体大小不同。
我试图将标签固定到 top/bottom,但这并不适用于所有情况。
我应该如何垂直放置标签?
要查看会发生什么,请将标签的背景设为黄色。您的约束不明确。
要修复它,请删除最后一个垂直约束。你不需要它。
这在我的测试场上有效:
let titleLabel = UILabel()
titleLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
titleLabel.font = UIFont.boldSystemFontOfSize(30)
titleLabel.text = "title"
hostView.addSubview(titleLabel)
let descriptionLabel = UILabel()
descriptionLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
descriptionLabel.font = UIFont.boldSystemFontOfSize(20)
descriptionLabel.text = "description"
hostView.addSubview(descriptionLabel)
let views = ["title": titleLabel, "description": descriptionLabel]
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[title]-2-[description]-(>=5)-|", options: NSLayoutFormatOptions.AlignAllFirstBaseline, metrics: nil, views: views))
NSLayoutConstraint(item: titleLabel, attribute: .CenterY, relatedBy: .Equal, toItem: hostView, attribute: .CenterY, multiplier: 1.0, constant: 0.0).active = true
结果:
您可以使用 AttributedString,而不是为富文本使用两个不同的标签。这是一个例子:
- (NSMutableAttributedString*)getRichText {
NSString *str1 = @"I am bold ";
NSString *str2 = @"I am simple";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:[str1 stringByAppendingString:str2]];
UIFont *font1=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
UIFont *font2=[UIFont fontWithName:@"Helvetica" size:20.0f];
NSInteger l1 = str1.length;
NSInteger l2 = str2.length;
[attString addAttribute:NSFontAttributeName value:font1 range:NSMakeRange(0,l1)];
[attString addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(l1,l2)];
return attString;
}
在 View did load 中,您可以将字符串设置为标签,如下所示:
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
[self.view addSubview:textLabel];
textLabel.attributedText = [self getRichText];
输出:
您需要做的很简单。
你们两个标签(对象)只需要具有相同的高度。
为此,请为您的第一个标签添加约束(例如 40 像素)。
完成后选择第一个和第二个标签,然后在屏幕底部的约束菜单中选择(左边的那个,添加新的对齐约束)select 顶部边缘和底部边缘。
然后您可以根据需要设置顶部宽度等。
尽情享受
我有一个父对象 UIView
,我在其中放置了两个标签。这些标签中的每一个都只有一行,如下所示:
现在的问题是基线不对。我正在使用自动布局,问题是在这种情况下我的约束应该是什么样子?特别是标签的垂直定位。这些是我目前的限制条件:
H:|-[title]-2-[description]-(>=5)-| //with NSLayoutFormatOptions.AlignAllFirstBaseline
V:|[title]|
V:|[description]|
以上约束导致
Unable to simultaneously satisfy constraints.
因为居中约束和第一个基线约束相互冲突。标签应占据父级的全高,但字体大小不同。
我试图将标签固定到 top/bottom,但这并不适用于所有情况。
我应该如何垂直放置标签?
要查看会发生什么,请将标签的背景设为黄色。您的约束不明确。
要修复它,请删除最后一个垂直约束。你不需要它。
这在我的测试场上有效:
let titleLabel = UILabel()
titleLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
titleLabel.font = UIFont.boldSystemFontOfSize(30)
titleLabel.text = "title"
hostView.addSubview(titleLabel)
let descriptionLabel = UILabel()
descriptionLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
descriptionLabel.font = UIFont.boldSystemFontOfSize(20)
descriptionLabel.text = "description"
hostView.addSubview(descriptionLabel)
let views = ["title": titleLabel, "description": descriptionLabel]
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[title]-2-[description]-(>=5)-|", options: NSLayoutFormatOptions.AlignAllFirstBaseline, metrics: nil, views: views))
NSLayoutConstraint(item: titleLabel, attribute: .CenterY, relatedBy: .Equal, toItem: hostView, attribute: .CenterY, multiplier: 1.0, constant: 0.0).active = true
结果:
您可以使用 AttributedString,而不是为富文本使用两个不同的标签。这是一个例子:
- (NSMutableAttributedString*)getRichText {
NSString *str1 = @"I am bold ";
NSString *str2 = @"I am simple";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:[str1 stringByAppendingString:str2]];
UIFont *font1=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
UIFont *font2=[UIFont fontWithName:@"Helvetica" size:20.0f];
NSInteger l1 = str1.length;
NSInteger l2 = str2.length;
[attString addAttribute:NSFontAttributeName value:font1 range:NSMakeRange(0,l1)];
[attString addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(l1,l2)];
return attString;
}
在 View did load 中,您可以将字符串设置为标签,如下所示:
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
[self.view addSubview:textLabel];
textLabel.attributedText = [self getRichText];
输出:
您需要做的很简单。 你们两个标签(对象)只需要具有相同的高度。
为此,请为您的第一个标签添加约束(例如 40 像素)。 完成后选择第一个和第二个标签,然后在屏幕底部的约束菜单中选择(左边的那个,添加新的对齐约束)select 顶部边缘和底部边缘。
然后您可以根据需要设置顶部宽度等。
尽情享受