[英]How to create a multiline UITextfield?
I am developing an application where user has to write some information.我正在开发一个应用程序,用户必须在其中编写一些信息。 For this purpose I need a UITextField
which is multi-line (in general UITextField
is a single line).为了这个目的,我需要一个UITextField
其是多行(在一般UITextField
是一个单一的线)。
As I'm Googling I find a answer of using UITextView
instead of UITextfield
for this purpose.当我在谷歌搜索时,我找到了为此目的使用UITextView
而不是UITextfield
的答案。
UITextField
is specifically one-line only. UITextField
特别是一行。
Your Google search is correct, you need to use UITextView
instead of UITextField
for display and editing of multiline text.你的谷歌搜索是正确的,你需要使用UITextView
而不是UITextField
来显示和编辑多行文本。
In Interface Builder, add a UITextView
where you want it and select the "editable" box.在 Interface Builder 中,在需要的位置添加UITextView
并选择“可编辑”框。 It will be multiline by default.默认情况下它将是多行的。
You can fake a UITextField using UITextView.您可以使用 UITextView 伪造 UITextField。 The problem you'll have is that you lose the place holder functionality.您将遇到的问题是您失去了占位符功能。
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
Ok I did it with some trick ;) First build a UITextField
and increased it's size
like this :好的,我用了一些技巧;) 首先构建一个UITextField
并增加它的size
如下所示:
CGRect frameRect = textField.frame;
frameRect.size.height = 53;
textField.frame = frameRect;
Then build a UITextView
exactly in the same area that u made my UITextField
, and deleted its background color
.然后在您创建UITextField
的同一区域构建一个UITextView
,并删除其background color
。 Now it looks like that u have a multiple lines TextField
!现在看起来你有一个多行TextField
!
Besides from the multiple line behaviour, the main difference between UITextView and UITextField is that the UITextView does not propose a placeholder.除了多行行为之外,UITextView 和 UITextField 之间的主要区别在于 UITextView 不建议占位符。 To bypass this limitation, you can use a UITextView with a "fake placeholder."要绕过此限制,您可以使用带有“假占位符”的 UITextView。
See this SO question for details: Placeholder in UITextView .有关详细信息,请参阅此 SO 问题: Placeholder in UITextView 。
Yes, a UITextView is what you're looking for.是的,一个 UITextView 就是你要找的。 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.您必须以不同的方式处理某些事情(例如返回键),但您可以向其中添加文本,如果里面有太多文本,它将允许您上下滚动。
This link has info about making a screen to enter data:此链接包含有关制作屏幕以输入数据的信息:
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.如果您必须有一个带有 2 行文本的 UITextField,一种选择是添加一个 UILabel 作为第二行文本的 UITextField 的子视图。 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.我的应用程序中有一个 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];
使用 UITextView 而不是 UITextField
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.补充上面h4xxr的回答,更简单的调整UITextField高度的方法是在属性inspectors->Text Field中选择方形边框样式。 (By default, the border style of a UITextfield is ellipse.) (默认情况下,UITextfield 的边框样式为椭圆。)
Reference: Answered Brian in here : How to set UITextField height?参考:在这里回答布赖恩: 如何设置 UITextField 高度?
This code block is enough.这个代码块就够了。 Please don't forget to set delegate in viewDidLoad or by storyboard just before to use the following extension:请不要忘记在使用以下扩展之前在 viewDidLoad 或故事板中设置委托:
extension YOUR_VIEW_CONTROLLER: UITextViewDelegate {
func textViewDidBeginEditing (_ textView: UITextView) {
if YOUR_TEXT_VIEW.text.isEmpty || YOUR_TEXT_VIEW.text == "YOUR DEFAULT PLACEHOLDER TEXT HERE" {
YOUR_TEXT_VIEW.text = nil
YOUR_TEXT_VIEW.textColor = .red // YOUR PREFERED COLOR HERE
}
}
func textViewDidEndEditing (_ textView: UITextView) {
if YOUR_TEXT_VIEW.text.isEmpty {
YOUR_TEXT_VIEW.textColor = UIColor.gray // YOUR PREFERED PLACEHOLDER COLOR HERE
YOUR_TEXT_VIEW.text = "YOUR DEFAULT PLACEHOLDER TEXT HERE"
}
}
}
使用textView
代替然后用其委托符合,调用textViewDidChange
该方法调用的方法中tableView.beginUpdates()
和tableView.endUpdates()
不要忘记设置rowHeight
和estimatedRowHeight
到UITableView.automaticDimension
。
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这将强制一行文本呈现
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.