我想在textfield键入文本时移动占位符文本
我使用以下代码
UITextField *email =[[UITextField alloc]initWithFrame:CGRectMake(48, 330, 330, 50)];
UITextField *password =[[UITextField alloc]initWithFrame:CGRectMake(48, 400, 330, 50)];
email.backgroundColor = [UIColor whiteColor];
email.font = [UIFont fontWithName:@"Arial-BoldMT" size:22];
email.textColor = [UIColor blackColor];
email.keyboardType = UIKeyboardTypeEmailAddress;
email.spellCheckingType = YES;
email.layer.cornerRadius =10.5;
email.clipsToBounds = YES;
email.placeholder=@"Email address";
email.textAlignment=NSTextAlignmentLeft;
email.layer.borderWidth = 1;
email.layer.borderColor = [[UIColor blackColor] CGColor];
I used the following code
UITextField *email =[[UITextField alloc]initWithFrame:CGRectMake(48, 330, 330, 50)];
UITextField *password =[[UITextField alloc]initWithFrame:CGRectMake(48, 400, 330, 50)];
email.backgroundColor = [UIColor whiteColor];
email.font = [UIFont fontWithName:@"Arial-BoldMT" size:22];
email.textColor = [UIColor blackColor];
email.keyboardType = UIKeyboardTypeEmailAddress;
email.spellCheckingType = YES;
email.layer.cornerRadius =10.5;
email.clipsToBounds = YES;
email.placeholder=@"Email address";
email.textAlignment=NSTextAlignmentLeft;
email.layer.borderWidth = 1;
email.layer.borderColor = [[UIColor blackColor] CGColor];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信,OP想要的是类似于输入的HTML5 +引导行为,因为当用户开始键入占位符文本时,占位符时(或上方)。 iOS中的行为没有默认/构建(我知道)。
即使您没有对此表示任何尝试(您应该这样做,并且人们想在尝试提供帮助之前就尝试尝试 - 我也希望至少尝试一次尝试),我有一些初步的建议/帮助您应该指向您要完成的工作的正确方向:
我建议创建一个自定义视图,该视图与您的测试场以及另一个标签一起使用(该标签将用于显示占位符时,用户开始在文本字段中键入)。
在XIB中,将标签设置为高于文本字段(因此在顶部显示)。
将“占位符”的底部约束设置为“ textfield”的顶部,“ textfield”偏移了“ placeholderLabel”的高度,将此约束连接到您的自定义视图上的iboutlet,我们将在以后使用<<代码>垂直空格constraintforplaceholder whelediting 。
然后使您的自定义视图(持有两个元素)textfielddelegate并实现
- (bool)textfield :( uitextfield *)textfield shordchangecharactersinrange :( nsrange)range range replasementsTring:(nsstring *)代码>根据文本字段是否具有占位符文本(是否具有任何内容)来确定是否显示或隐藏占位符标签 - textfield的IBOUTLET是公开的(在.h中),因为它可能需要要通过其他类访问:
这是使用上述代码的示例:
I believe what OP wants is something akin to the HTML5 + bootstrap behavior of inputs in that when the user starts typing the placeholder text moves up to the top line of the input field (or above it). There is no default/build in behavior for this in iOS (that I know of).
Even though you didn't show any attempt at this (and you should as this is SO and folks want to see attempts before trying to help -- I would have liked to see at least an attempt as well), I've got some preliminary advice/help for you which should point you in the right direction of what you'd like to accomplish:
I'd recommend creating a custom view which holds your testField along with another label (the label will be used for displaying the placeholder when the user starts typing in the text field).
In the xib, set the label higher up than the text field (so it displays on top).
Set the bottom constraint of the "placeholderLabel" to the top of the "textField" offset by the height of the "placeholderLabel" Connect this constraint to an IBOutlet on your custom view which we're going to use later
verticalSpacingConstraintForPlaceholderWhileEditing
.Then make your custom view (which is holding both elements) a textFieldDelegate and implement
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
to determine whether to show or hide the placeholder label based on whether the text field is going to have placeholder text or not (whether it has any contents) -- IBOutlet for textField is public (in the .h) as it may need to be accessed by other classes:Here's an example using the above code: