ios 如何创建多行 UITextfield?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1345561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:21:01  来源:igfitidea点击:

How to create a multiline UITextfield?

iosuitextfielduitextview

提问by raaz

I am developing an application where user has to write some information. For this purpose I need a UITextFieldwhich is multi-line (in general UITextFieldis a single line).

我正在开发一个应用程序,用户必须在其中编写一些信息。为此,我需要一个UITextField多行(通常UITextField是单行)。

As I'm Googling I find a answer of using UITextViewinstead of UITextfieldfor this purpose.

当我在谷歌搜索时,我找到了使用UITextView而不是UITextfield为此目的的答案。

回答by h4xxr

UITextFieldis specifically one-line only.

UITextField特别是只有一行。

Your Google search is correct, you need to use UITextViewinstead of UITextFieldfor display and editing of multiline text.

您的 Google 搜索是正确的,您需要使用UITextView代替UITextField来显示和编辑多行文本。

In Interface Builder, add a UITextViewwhere you want it and select the "editable" box. It will be multiline by default.

在 Interface Builder 中,UITextView在您想要的位置添加一个并选择“可编辑”框。默认情况下它将是多行的。

回答by Dave G

You can fake a UITextField using UITextView. The problem you'll have is that you lose the place holder functionality.

您可以使用 UITextView 伪造 UITextField。您将遇到的问题是您失去了占位符功能。

If you choose to use a UITextView and need the placeholder, do this:

如果您选择使用 UITextView 并需要占位符,请执行以下操作:

In your viewDidLoad set the color and text to placeholders:

在您的 viewDidLoad 中,将颜色和文本设置为占位符:

myTxtView.textColor = .lightGray
myTxtView.text = "Type your thoughts here..."

Then make the placeholder disappear when your UITextView is selected:

然后在选择 UITextView 时使占位符消失:

func textViewDidBeginEditing (textView: UITextView) {
    if myTxtView.textColor.textColor == ph_TextColor && myTxtView.isFirstResponder() {
        myTxtView.text = nil
        myTxtView.textColor = .white
    }
}

When the user finishes editing, ensure there's a value. If there isn't, add the placeholder again:

当用户完成编辑时,确保有一个值。如果没有,请再次添加占位符:

func textViewDidEndEditing (textView: UITextView) {
    if myTxtView.text.isEmpty || myTxtView.text == "" {
        myTxtView.textColor = .lightGray
        myTxtView.text = "Type your thoughts here..."
    }
}

Other features you might need to fake:

您可能需要伪造的其他功能:

UITextField's often capitalize every letter, you can add that feature to UITableView:

UITextField 通常将每个字母大写,您可以将该功能添加到 UITableView:

myTxtView.autocapitalizationType = .words

UITextField's don't usually scroll:

UITextField 通常不会滚动:

myTxtView.scrollEnabled = false

回答by Rudi

Ok I did it with some trick ;) First build a UITextFieldand increased it's sizelike this :

好的,我用一些技巧做到了;) 首先构建一个UITextField并增加它是size这样的:

CGRect frameRect = textField.frame;
        frameRect.size.height = 53;
        textField.frame = frameRect;

Then build a UITextViewexactly in the same area that u made my UITextField, and deleted its background color. Now it looks like that u have a multiple lines TextField!

然后在你创建的UITextView完全相同的区域中构建一个UITextField,并删除它的background color. 现在看起来你有多行TextField

回答by MonsieurDart

Besides from the multiple line behaviour, the main difference between UITextView and UITextField is that the UITextView does not propose a placeholder. To bypass this limitation, you can use a UITextView with a "fake placeholder."

除了多行行为之外,UITextView 和 UITextField 之间的主要区别在于 UITextView 不建议占位符。要绕过此限制,您可以使用带有“假占位符”的 UITextView。

See this SO question for details: Placeholder in UITextView.

有关详细信息,请参阅此 SO 问题:Placeholder in UITextView

回答by nevan king

Yes, a UITextView is what you're looking for. You'll have to deal with some things differently (like the return key) but you can add text to it, and it will allow you to scroll up and down if there's too much text inside.

是的,一个 UITextView 就是你要找的。您必须以不同的方式处理某些事情(例如返回键),但您可以向其中添加文本,如果里面有太多文本,它将允许您上下滚动。

This link has info about making a screen to enter data:

此链接包含有关制作屏幕以输入数据的信息:

create a data entry screen

创建数据输入屏幕

回答by DiscDev

If you must have a UITextField with 2 lines of text, one option is to add a UILabel as a subview of the UITextField for the second line of text. I have a UITextField in my app that users often do not realize is editable by tapping, and I wanted to add some small subtitle text that says "Tap to Edit" to the UITextField.

如果您必须有一个带有 2 行文本的 UITextField,一种选择是添加一个 UILabel 作为第二行文本的 UITextField 的子视图。我的应用程序中有一个 UITextField,用户通常没有意识到可以通过点击进行编辑,我想向 UITextField 添加一些小字幕文本,上面写着“点击编辑”。

CGFloat tapLlblHeight = 10;
UILabel *tapEditLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, textField.frame.size.height - tapLlblHeight - 2, 70, tapLlblHeight)];
tapEditLbl.backgroundColor = [UIColor clearColor];
tapEditLbl.textColor = [UIColor whiteColor];
tapEditLbl.text = @"Tap to Edit";

[textField addSubview:tapEditLbl];

回答by Monir Khlaf

use UITextView instead of UITextField

使用 UITextView 而不是 UITextField

回答by Tony

A supplement to h4xxr's answer in the above, an easier way to adjust the height of the UITextField is to select square border style in the attribute inspectors->Text Field. (By default, the border style of a UITextfield is ellipse.)

补充上面h4xxr的回答,更简单的调整UITextField高度的方法是在属性inspectors->Text Field中选择方形边框样式。(默认情况下,UITextfield 的边框样式为椭圆。)

Reference: Answered Brian in here : How to set UITextField height?

参考:在这里回答布赖恩:如何设置 UITextField 高度?

回答by McFlys

There is another option that worked for me:

还有另一种选择对我有用:

Subclass UITextField and overwrite:

子类化 UITextField 并覆盖:

- (void)drawTextInRect:(CGRect)rect

In this method you can for example:

例如,在此方法中,您可以:

    NSDictionary *attributes = @{ NSFontAttributeName : self.font,
                                  NSForegroundColorAttributeName : self.textColor };

    [self.text drawInRect:verticalAlignedRect withAttributes:attributes];

This code will render the text using as many lines as required if the rect has enough space. You could specify any other attribute depending on your needs.

如果矩形有足够的空间,此代码将根据需要使用尽可能多的行来呈现文本。您可以根据需要指定任何其他属性。

Do not use:

不使用:

self.defaultTextAttributes

which will force one line text rendering

这将强制一行文本呈现