如何调整 UIView 的大小以适应一定数量的 UILabels 并调整 UILabels 的大小以适合文本?
我有一个 UIView,它应该包含 UILabels,并且如果 UILabel 为零,则这些 UILabels 不固定,我不显示它,如果不是,我必须调整 UILabel 的大小以适合文本(它们可以是 2 ligne)并放置它位于另一个 UILabel 下,并且在我必须调整 UIView 容器的大小以适应此 UILabels 的数量之后。这是我想以编程方式执行的屏幕截图:
I have an UIView that it should contains UILabels, and these UILabels are not fixed if an UILabel is nil, i don't display it, and if is not i must resize the UILabel to fit text (they can be 2 ligne) and put it under another UILabel, and after i must resize the UIView container for the amount of this UILabels. this the Screen Shot that i want to do programmatically:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
UILabels 可以通过自动缩小字体来容纳大量文本(请参阅 Interface Builder 中的最小字体大小值)。我会使用 UITextViews,而不是标签。它们通过允许用户滚动来容纳大量文本。要使它们隐藏,请更改 UITextViews 的隐藏选择器属性。由于 UITextView 的大小永远不会改变,因此您可以轻松计算每个 UITextView 将 UIView 容器的高度减少多少。希望这有帮助!
UILabels can fit large amounts of text by automatically making the font smaller (see the minimum font size value in the Interface Builder). Instead of labels, I would use UITextViews. They fit large amounts of text by allowing the user to scroll. To make them hidden, use change the hidden selector attribute of the UITextViews. Because the UITextViews will never change in size, you can easily calculate how much to decrease the height of the UIView container by per UITextView. Hope this helps!
我认为你必须一一计算标签的高度(更具体地说,是原始高度的加法)。当然,您应该能够通过将所有标签放在一个数组中来系统地完成此操作。
您可以使用 [NSString sizeWithFont:] 方法(有几个具有相似名称的方法)来计算高度。
http://developer.apple.com/库/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html
I think you have to calculate the height of the labels (more specifically, the addition from the original height) one by one. Of course you should be able to do this systematically by having all the labels in an array.
You can use [NSString sizeWithFont:] methods (there are several with similar names) to calculate the height.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html
如果您想保持相同的字体大小,那么您可以根据文本的长度在运行时计算标签的框架大小。
参见此处
If you want to keep the same font size then you can calculate the frame size for the label at run time depending upon the length of the text..
See here
您需要使用 label-
的以下两个属性,当您使用这两个属性时,您将实现您想要的效果。
You need to used following two properties of label-
when you use both of these properties then you will achieve what you want.