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

查看:21
本文介绍了使用自动布局使用文本水平调整 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 事件,我触发了内在内容大小的重置:

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

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 子类,可以在其中设置 intrinsicContentSize,覆盖 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天全站免登陆