ios 为什么 UITextview 没有像 UITextField 这样的占位符属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15881454/
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
Why UITextview does not have placeholder property like UITextField
提问by Mahesh Peri
Hi I am working with iPhone apps, I know the UITextfield has placeholder property. But UITextview doesn't have placeholder property. I know the process how to customize placeholder for UITextview. But please let me know the difference.
嗨,我正在使用 iPhone 应用程序,我知道 UITextfield 具有占位符属性。但是 UITextview 没有占位符属性。我知道如何为 UITextview 自定义占位符的过程。但请让我知道其中的区别。
and UITextfield inherits from UIControl which inturns inherits from UIView...NSObject. and UITextview inherits from UIScrollView, UIView,UIresponder...NSOBject. But where is the difference frome place holder property.
UITextfield 继承自 UIControl,而 UIControl 又继承自 UIView...NSObject。而 UITextview 继承自 UIScrollView、UIView、UIresponder...NSOBject。但是与占位符属性的区别在哪里。
How textfield contains placeholder property, why textview doesn't contain placeholder?
文本字段如何包含占位符属性,为什么文本视图不包含占位符?
Please give any ideas, suggestions, and/or give better explanation.
请给出任何想法、建议和/或给出更好的解释。
回答by sixthcent
I don't know why Apple did not include a placeHolder attribute on UITextView. But I was able to add one using a UILabel subview that contains the placeholder text.
我不知道为什么 Apple 没有在 UITextView 上包含 placeHolder 属性。但是我能够使用包含占位符文本的 UILabel 子视图添加一个。
Then show or hide it from delegate callbacks as below
然后在委托回调中显示或隐藏它,如下所示
- (void)textViewDidBeginEditing:(UITextView *)textView
{
self.defaultLabel.hidden = YES;
}
- (void)textViewDidChange:(UITextView *)txtView
{
self.defaultLabel.hidden = ([txtView.text length] > 0);
}
- (void)textViewDidEndEditing:(UITextView *)txtView
{
self.defaultLabel.hidden = ([txtView.text length] > 0);
}
回答by DBD
Why UITextField
has a placeholder value and UITextView
doesn't has nothing to do with with their inheritance, or even programming. It's about how Apple perceived their use and prefers their use.
为什么UITextField
具有占位符值并且UITextView
与它们的继承甚至编程无关。这是关于苹果如何看待他们的使用和更喜欢他们的使用。
UITextField
is a one line item. It can be used for entering usernames, passwords, address bits, or pretty much anything. Often you have several of them clumped together, and it might be hard to figure out which field should have which data without placeholder text. The place holder lets us know the purpose of the field.
UITextField
是一个单行项目。它可用于输入用户名、密码、地址位或几乎任何内容。通常,您将其中的几个聚集在一起,如果没有占位符文本,可能很难确定哪个字段应该包含哪些数据。占位符让我们知道该字段的用途。
UITextView
is a block of text. Usually several lines, which often have a header associated with them. As a result, placeholder text is often not useful. The text view is also more likely to be populated with static text which again, would make placeholder text not really useful.
UITextView
是一个文本块。通常是几行,通常有一个标题与之相关联。因此,占位符文本通常没有用。文本视图也更有可能填充静态文本,这又会使占位符文本没有真正的用处。
Do I think Apple should add a placeholder property for UITextView
anyway? Yes. It's annoying to have to make my own on the occasion when I need it. Apple also uses placeholder text in some places where it uses a UITextView
. The following example is from adding a calendar entry.
我是否认为 Apple 应该添加占位符属性UITextView
?是的。在我需要的时候不得不自己做,这很烦人。Apple 还在某些使用UITextView
. 以下示例来自添加日历条目。