水平调整的UITextField使用自动布局文本 [英] Resize UITextField horizontally with text using Auto Layout
问题描述
这是示例项目来演示跌破发行可以在这里找到:<一href=\"https://github.com/markdorison/UITextFieldContentSizeExample/\">https://github.com/markdorison/UITextFieldContentSizeExample/
我试图有一个的UITextField成长/它包含了使用自动布局的文本内容缩减其宽度。我创建了领先,并与749优先尾随约束(而不是默认:1000)。我也曾尝试降低这些限制的优先一路下降到1,但是这似乎并没有对行为产生显着影响。
通过听取UIControlEventEditingChanged事件,我触发intrinsicContentSize的复位:
- (IBAction为)textFieldDidChange:(*的UITextField)文本框{
[UIView的animateWithDuration:0.1动画:^ {
[文本框invalidateIntrinsicContentSize]
}];
}
此方法的几乎的工作。这里有两个问题:
-
我设置的占位符。然而宽的占位符是,所述的UITextField的intrinsicContentSize的宽度决不会低于它。所以,如果占位符的宽度是130,我开始键入,文本视图的宽度将保持在130到什么我打字达到的宽度比130越大;在此之后的文本视图将增长。
-
在我的例子中,我已经放在下面的UITextField一个UITextView。当我在键入的UITextField,其宽度的增长预期。那么我可以挖掘到的UITextView和编辑文本。如果我再回去和编辑的UITextView,它的宽度不再增长/收缩预期。该值被从被标准的前瞻性整数像
{192,28}
,到{191.536,27.959999} 返回由的UITextField的intrinsicContentSize财产去code>。
任何帮助将是AP preciated!
我设置的占位符。然而宽的占位符是,所述的UITextField的intrinsicContentSize的宽度决不会低于它。所以,如果占位符的宽度是130,我开始键入,文本视图的宽度将保持在130到什么我打字达到的宽度比130越大;在此之后的文本视图将增长。
块引用>我认为你将不得不使用一个子类的UITextField,其中的您的获取将
instrinsicContentSize
,覆盖的默认行为intrinsicContentSize
方法,它显然是大小根据占位符。An example project to demonstrate the below issue can be found here: https://github.com/markdorison/UITextFieldContentSizeExample/
I am attempting to have a UITextField grow/shrink its width with the text content it contains using Auto Layout. I have created leading and trailing constraints with a priority of 749 (as opposed to the default: 1000). I have also tried lowering the priority of these constraints all the way down to 1, but this does not seem to have a noticeable effect on the behavior.
By listening to the UIControlEventEditingChanged event, I am triggering a reset of the intrinsicContentSize:
- (IBAction)textFieldDidChange:(UITextField *)textField { [UIView animateWithDuration:0.1 animations: ^{ [textField invalidateIntrinsicContentSize]; }]; }
This approach almost works. There are two issues:
I am setting a placeholder. However wide the placeholder is, the width of the UITextField's intrinsicContentSize will never drop below it. So if the placeholder's width is 130, and I begin to type, the text view's width will remain at 130 until what I am typing reaches a width greater than 130; after this the text view will grow.
In my example I have placed a UITextView below the UITextField. When I type in the UITextField, its width grows as expected. I can then tap into the UITextView and edit the text. If I then go back and edit the UITextView, its width no longer grows/shrinks as expected. The values being returned by the UITextField's intrinsicContentSize property go from being standard-looking integers like
{192, 28}
, to{191.536, 27.959999}
.Any help would be appreciated!
解决方案I am setting a placeholder. However wide the placeholder is, the width of the UITextField's intrinsicContentSize will never drop below it. So if the placeholder's width is 130, and I begin to type, the text view's width will remain at 130 until what I am typing reaches a width greater than 130; after this the text view will grow.
I think you'll have to use a UITextField subclass where you get to set the
instrinsicContentSize
, overriding the default behavior of theintrinsicContentSize
method which is evidently to size based on the placeholder.这篇关于水平调整的UITextField使用自动布局文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!