简体   繁体   English

如何估计UITextField的正确高度,以保存给定字体大小的文本

[英]How to estimate the proper height of a UITextField, to hold text of given font size

I want to display a single-line text field using UITextField and I need to know before displaying it, the proper size for its containing UICollectionViewCell. 我想使用UITextField显示单行文本字段,我需要在显示它之前知道它包含UICollectionViewCell的正确大小。 The text can be one of multiple font sizes and I need to get the right height for displaying it comfortably. 文本可以是多种字体大小之一,我需要获得正确的高度以便舒适地显示它。

Since the text is not known in advance (it can be edited by the user), I can't use NSAttributedString 's -size and -boundingRectWithSize:options:context: with anything but dummy text, in which case I can't really trust the resulting size to hold any piece of text, right? 由于文本事先不知道(它可以由用户编辑),我不能使用NSAttributedString-size-boundingRectWithSize:options:context:除了虚拟文本之外的任何东西,在这种情况下我不能真的相信最终的大小可以容纳任何文本,对吧?

I guess my question is: Is there a rule of thumb about typography in general, or some useful API I'm not aware of, that would allow me to determine that for displaying text at X pt, I need a text field with a height of Y px. 我想我的问题是:是否有关于排版的一般经验法则,或者我不知道的一些有用的API,这将允许我确定在X pt显示文本,我需要一个高度的文本字段Y px。

UITextField implements the sizeThatFits: method. UITextField实现sizeThatFits:方法。 So the most reliable way to get the size of your text field is to actually create one, set it up like you would set up your real text fields, and ask it for a suitable size. 因此,获得文本字段大小的最可靠方法是实际创建一个,设置它就像设置真实文本字段一样,并要求它获得合适的大小。 You don't even have to give it placeholder text, because UITextField will choose the size based on its font, not on its text. 你甚至不必给它占位符文本,因为UITextField将根据其字体而不是文本来选择大小。

UITextField *dummy = [[UITextField alloc] init];
dummy.font = [UIFont systemFontOfSize:20];
dummy.borderStyle = UITextBorderStyleRoundedRect;
CGFloat requiredHeight = [dummy sizeThatFits:CGSizeMake(HUGE_VALF, HUGE_VALF)].height;
// requiredHeight == 30 in my test

If your deployment target is iOS 6.0 or later, you can instead use the intrinsicContentSize property, like this: 如果您的部署目标是iOS 6.0或更高版本,则可以改为使用intrinsicContentSize属性,如下所示:

CGFloat requiredHeight = dummy.intrinsicContentSize.height;
// requiredHeight == 30 in my test

Note that sizeThatFits: still works in iOS 6, but intrinsicContentSize is a little easier to understand. 请注意, sizeThatFits:仍可在iOS 6中运行,但intrinsicContentSize更容易理解。

iOS always returns a height of 30 units when the borderStyle is RoundedRect. 当borderStyle为RoundedRect时,iOS总是返回30个单位的高度。 If you want to compute the height required by a custom font, you have to change the borderStyle to any other value, compute the height, and change the borderStyle back. 如果要计算自定义字体所需的高度,则必须将borderStyle更改为任何其他值,计算高度,然后更改borderStyle。

textField.borderStyle = UITextBorderStyle.None;
let sizeThatFits = textField.sizeThatFits(CGSize(width:textField.bounds.width, height: CGFloat.max));
textField.bounds.height = sizeThatFits.height;
textField.borderStyle = UITextBorderStyle.RoundedRect;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM