使用自动布局使用文本水平调整UITextField的大小 [英] Resize UITextField horizontally with text using Auto Layout

查看:482
本文介绍了使用自动布局使用文本水平调整UITextField的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是可演示以下问题的示例项目: https://github.com/markdorison / UITextFieldContentSizeExample /

An example project to demonstrate the below issue can be found here: https://github.com/markdorison/UITextFieldContentSizeExample/

我试图有一个UITextField增加/收缩其宽度与它包含使用自动布局的文本内容。我已经创建了前导和尾随约束,优先级为749(而不是默认值:1000)。我也尝试将这些约束的优先级降低到1,但这似乎没有对行为有显着的影响。

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.

通过侦听UIControlEventEditingChanged事件,我触发了重置intrinsicContentSize:

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:


  1. 我设置了一个占位符。不管宽度占位符是多少,UITextField的intrinsicContentSize的宽度将永远不会低于它。因此,如果占位符的宽度为130,并且我开始键入,文本视图的宽度将保持在130,直到我正在输入的宽度大于130;

  1. 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.

在我的示例中,我在UITextField下放置了一个UITextView。当我在UITextField中输入时,其宽度按预期增长。然后我可以点击进入UITextView并编辑文本。如果我回去编辑UITextView,它的宽度不再像预期的那样增长/收缩。 UITextField的intrinsicContentSize属性返回的值从 {192,28} 等标准整型变为 {191.536,27.959999} code>。

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}.

任何帮助将不胜感激。

推荐答案


我设置了一个占位符。不管宽度占位符是多少,UITextField的intrinsicContentSize的宽度将永远不会低于它。因此,如果占位符的宽度为130,并且我开始键入,文本视图的宽度将保持在130,直到我正在输入的宽度大于130;

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.

我想你必须使用UITextField子类, >获取设置 instrinsicContentSize ,覆盖 intrinsicContentSize 方法的默认行为,这显然是基于占位符。

I think you'll have to use a UITextField subclass where you get to set the instrinsicContentSize, overriding the default behavior of the intrinsicContentSize method which is evidently to size based on the placeholder.

这篇关于使用自动布局使用文本水平调整UITextField的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆