iOS 中的标签高度问题?

问题描述 投票:0回答:1

我在父视图中垂直添加了 4 个视图。我给了他们以下约束

  1. 与父母等宽
  2. 固定高度70
  3. 1
  4. 视图之间的垂直边距
  5. 父视图的中心 x

现在标签中的文本可以增长它不是静态的。截至目前,标签内的文本显示垂直居中所以我添加了下面的代码以使文本与标签顶部对齐而不显示在标签的垂直中心(代码来自here )

//Calculate the expected size based on the font and linebreak mode of your label
// FLT_MAX here simply means no constraint in height
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];   

//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

如果我将上面的代码与标签一起使用,那么它会根据某些文本给出 67.201 的高度,但高度应该更高,因为文本更多但如果我不使用上面的代码,那么文本将垂直放置在标签内。

我想让文本与标签顶部对齐而不是垂直居中。因此标签的高度应该降低它不应该有任何顶部,底部填充。

ios objective-c iphone ipad
1个回答
0
投票

您不能将文本对齐到标签的顶部,只能降低其整体高度。 您可以使用具有该固定高度的容器视图来模拟效果,并让标签在该视图内自由调整。

看这个问题:Vertically align text to top within a UILabel

© www.soinside.com 2019 - 2024. All rights reserved.