App*_*Dev 38 multiline uitextfield uitextview ios swift
我需要向用户显示一个多行文本输入"框",其高度大于a的标准高度UITextField.什么是最好或最正确的方法应该是什么?:
UITextField并在代码中更改其高度或应用某个高度约束.UITextView.这是多行的,但默认情况下它没有占位符,我想我应该在代码中实现该功能.Dee*_*sia 82
UITextField 特别是一行.
请UITextView改用多行文字.
要在UITextView中实现占位符,请使用此逻辑/代码.
首先将UITextView设置为包含占位符文本,并将其设置为浅灰色,以模仿UITextField的占位符文本的外观.要么viewDidLoad在文本视图的创建中,要么在文本视图的创建中执行此操作.
对于斯威夫特
textView.text = "Placeholder"
textView.textColor = UIColor.lightGrayColor()
Run Code Online (Sandbox Code Playgroud)
对于Objective-C
textView.text = @"Placeholder";
textView.textColor =[UIColor lightGrayColor];
Run Code Online (Sandbox Code Playgroud)
然后,当用户开始编辑文本视图时,如果文本视图包含占位符(即,如果其文本颜色为浅灰色),则清除占位符文本并将文本颜色设置为黑色以适应用户的输入.
对于斯威夫特
func textViewDidBeginEditing(textView: UITextView) {
if textView.textColor == UIColor.lightGrayColor() {
textView.text = nil
textView.textColor = UIColor.blackColor()
}
}
Run Code Online (Sandbox Code Playgroud)
对于Objective-C
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
if (textView.textColor == [UIColor lightGrayColor]) {
textView.text = @"";
textView.textColor = [UIColor blackColor];
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
然后,当用户完成编辑文本视图并将其作为第一个响应者重新签名时,如果文本视图为空,则通过重新添加占位符文本并将其颜色设置为浅灰色来重置其占位符.
对于斯威夫特
func textViewDidEndEditing(textView: UITextView) {
if textView.text.isEmpty {
textView.text = "Placeholder"
textView.textColor = UIColor.lightGrayColor()
}
}
Run Code Online (Sandbox Code Playgroud)
对于Objective-C
- (void)textViewDidEndEditing:(UITextView *)textView{
if ([textView.text isEqualToString:@""]) {
textView.text = @"Placeholder";
textView.textColor =[UIColor lightGrayColor];
}
}
Run Code Online (Sandbox Code Playgroud)
还要UITextViewDelegate在视图控制器中添加.
Mr.*_*der 10
使用选项2,textField的高度无法更改,并且不显示第二行...
textView.text = "Placeholder"
textView.textColor = UIColor.lightGrayColor()
func textViewDidBeginEditing(textView: UITextView) {
if textView.textColor == UIColor.lightGrayColor() {
textView.text = nil
textView.textColor = UIColor.blackColor()
}
}
func textViewDidEndEditing(textView: UITextView) {
if textView.text.isEmpty {
textView.text = "Placeholder"
textView.textColor = UIColor.lightGrayColor()
}
}
Run Code Online (Sandbox Code Playgroud)
For Swift 3
UITextView doesn't inherently have a placeholder property so you'd have to create and manipulate one programmatically using UITextViewDelegate methods.
(Note: Add UITextViewDelegate to the class and set textView.delegate = self.)
First set the UITextView to contain the placeholder text and set it to a light gray color to mimic the look of a UITextField's placeholder text. Either do so in the viewDidLoad or upon the text view's creation.
textView.text = "Placeholder"
textView.textColor = UIColor.lightGray
Run Code Online (Sandbox Code Playgroud)
Then when the user begins to edit the text view, if the text view contains a placeholder (i.e. if its text color is light gray) clear the placeholder text and set the text color to black in order to accommodate the user's entry.
func textViewDidBeginEditing(_ textView: UITextView) {
if textView.textColor == UIColor.lightGray {
textView.text = nil
textView.textColor = UIColor.black
}
}
Run Code Online (Sandbox Code Playgroud)
Then when the user finishes editing the text view and it's resigned as the first responder, if the text view is empty, reset its placeholder by re-adding the placeholder text and setting its color to light gray.
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text.isEmpty {
textView.text = "Placeholder"
textView.textColor = UIColor.lightGray
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48245 次 |
| 最近记录: |