我想在textfield键入文本时移动占位符文本

发布于 2025-01-27 14:49:20 字数 661 浏览 5 评论 0原文

我使用以下代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东北女汉子 2025-02-03 14:49:20

我相信,OP想要的是类似于输入的HTML5 +引导行为,因为当用户开始键入占位符文本时,占位符时(或上方)。 iOS中的行为没有默认/构建(我知道)。

即使您没有对此表示任何尝试(您应该这样做,并且人们想在尝试提供帮助之前就尝试尝试 - 我也希望至少尝试一次尝试),我有一些初步的建议/帮助您应该指向您要完成的工作的正确方向:

我建议创建一个自定义视图,该视图与您的测试场以及另一个标签一起使用(该标签将用于显示占位符时,用户开始在文本字段中键入)。

在XIB中,将标签设置为高于文本字段(因此在顶部显示)。

将“占位符”的底部约束设置为“ textfield”的顶部,“ textfield”偏移了“ placeholderLabel”的高度,将此约束连接到您的自定义视图上的iboutlet,我们将在以后使用<<代码>垂直空格constraintforplaceholder whelediting 。

然后使您的自定义视图(持有两个元素)textfielddelegate并实现- (bool)textfield :( uitextfield *)textfield shordchangecharactersinrange :( nsrange)range range replasementsTring:(nsstring *)代码>根据文本字段是否具有占位符文本(是否具有任何内容)来确定是否显示或隐藏占位符标签 - textfield的IBOUTLET是公开的(在.h中),因为它可能需要要通过其他类访问:

@interface R4NShiftingPlaceholderInputView() <UITextFieldDelegate>
@property (strong, nullable) IBOutlet UILabel *placeholderWhenEditing;
@property (strong, nullable) IBOutlet NSLayoutConstraint *verticalSpacingConstraintForPlaceholderWhileEditing;
@end

@implementation R4NShiftingPlaceholderInputView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.placeholderWhenEditing.alpha = 0.0f;
    self.placeholderWhenEditing.text = self.textField.placeholder;
    self.textField.layer.cornerRadius = 10.5f;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // determine if we're going to have an empty string after
    NSString *completeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    CGFloat alphaValue = 0.0f;
    CGFloat animationSpeed = 0.07f;
    if (completeString.length > 0) {
        alphaValue = 1.0f;
        animationSpeed = 0.2f;
        // if you'd prefer to have it appear on top of the text field instead of near the top line, set the constraint to 0 instead (placeholderWhileEditing
        self.verticalSpacingConstraintForPlaceholderWhileEditing.constant = floorf(self.placeholderWhenEditing.frame.size.height / 2);
    } else {
        self.verticalSpacingConstraintForPlaceholderWhileEditing.constant = self.placeholderWhenEditing.frame.size.height;
    }
    [UIView animateWithDuration:animationSpeed animations:^{
        self.placeholderWhenEditing.alpha = alphaValue;
        [self layoutIfNeeded];
    }];
    return YES;
}

这是使用上述代码的示例:

”

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).

PlaceholderTextLabel

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.

BottomConstraintOfPlaceholder

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:

@interface R4NShiftingPlaceholderInputView() <UITextFieldDelegate>
@property (strong, nullable) IBOutlet UILabel *placeholderWhenEditing;
@property (strong, nullable) IBOutlet NSLayoutConstraint *verticalSpacingConstraintForPlaceholderWhileEditing;
@end

@implementation R4NShiftingPlaceholderInputView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.placeholderWhenEditing.alpha = 0.0f;
    self.placeholderWhenEditing.text = self.textField.placeholder;
    self.textField.layer.cornerRadius = 10.5f;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // determine if we're going to have an empty string after
    NSString *completeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    CGFloat alphaValue = 0.0f;
    CGFloat animationSpeed = 0.07f;
    if (completeString.length > 0) {
        alphaValue = 1.0f;
        animationSpeed = 0.2f;
        // if you'd prefer to have it appear on top of the text field instead of near the top line, set the constraint to 0 instead (placeholderWhileEditing
        self.verticalSpacingConstraintForPlaceholderWhileEditing.constant = floorf(self.placeholderWhenEditing.frame.size.height / 2);
    } else {
        self.verticalSpacingConstraintForPlaceholderWhileEditing.constant = self.placeholderWhenEditing.frame.size.height;
    }
    [UIView animateWithDuration:animationSpeed animations:^{
        self.placeholderWhenEditing.alpha = alphaValue;
        [self layoutIfNeeded];
    }];
    return YES;
}

Here's an example using the above code:

Example_Gif

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文