设置自定义 UITextField 最大长度 [英] Set custom UITextField max length

查看:40
本文介绍了设置自定义 UITextField 最大长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到解决我的问题的方法,因为 stackOverflow 上的所有其他帖子都参考到使用 IDE 而不是以编程方式创建的 UITextField.

I can't find a solution on my problem because all other posts at stackOverflow refer to UITextField created with the IDE and not programmatically.

我在 (void)viewDidLoad 内创建了一个自定义 UITextField.我以编程方式创建了它,因此我可以访问 leftView 属性.

I have a custom UITextField created inside (void)viewDidLoad.I have this created programmatically so i can access the leftView property.

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 66, screenAdj, 30)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.font=[UIFont fontWithName:@"Helvetica" size:14];
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textField.returnKeyType = UIReturnKeyDone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [textField addTarget:self action:@selector(textFieldShouldReturn:)forControlEvents:UIControlEventEditingDidEndOnExit];
    textField.text=[defaults stringForKey:@"username"];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    label.font=[UIFont fontWithName:@"Helvetica" size:14];
    label.text = @"Your name";
    label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = label;

我还想限制 TextField 实现中的最大字符数:

I also want to limit the max characters inside the TextField implementing:

- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSUInteger oldLength = [textField.text length];
    NSUInteger replacementLength = [string length];
    NSUInteger rangeLength = range.length;

    NSUInteger newLength = oldLength - rangeLength + replacementLength;

    BOOL returnKey = [string rangeOfString: @"\n"].location != NSNotFound;

    return newLength <= MAX_LENGTH_USERNAME || returnKey;
}

但我意识到我的 textField 没有看到"上述功能.尝试使用

But i realized that my textField doesn't "see" the above function.Tried with

[textField addTarget:self action:@selector(...

但没有运气.有什么帮助吗?

but no luck. Any help?

推荐答案

你必须告诉你的视图控制器符合 UITextFields 委托协议.在您的 (.h) 中,在UIViewController"之后添加 ,并在设置文本字段时将其委托设置为 self:

You have to tell your view controller to conform to UITextFields delegate protocol. In your (.h) add <UITextFieldDelegate> right after "UIViewController", and when you set up your text field set its delegate to self:

[textField setDelegate:self];

然后你可以继续删除这一行

Then you can go ahead and remove this line

[textField addTarget:self action:@selector(...

假设您使用它来调用 shouldChangeCharactersInRange.现在您已经设置了委托,该方法将被自动调用.

Assuming you were using this to call shouldChangeCharactersInRange. Now that you've set up the delegate, this method will be called automatically.

这篇关于设置自定义 UITextField 最大长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆