UITextField带有文本长度计数器

时间:2014-04-11 23:54:31

标签: ios objective-c uitextfield

好吧标题主要是:

是否有准备好的UITextField组件可以在标签中取一个数字(最大文本长度)?如果也可以显示数字也会很酷。

那里有组件吗?

我想拥有什么:

  1. 按自定义文字长度限制输入
  2. 在TextField
  3. 中显示左侧字符数
  4. 预防粘贴

3 个答案:

答案 0 :(得分:5)

请参阅我的其他答案以获得一个好的子组件。

我在TextField中添加了一个UILabel子视图来显示字符限制。

它将:
*按自定义文本长度限制输入
*在UITextField内的UILabel子视图中显示字符限制 *粘贴预防

结果: Empty textField (20 characters left) 要么 three characters (17 characters left)

@implementation ViewController
@synthesize textField1;
UILabel *yourLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(textField1.bounds.size.width-18, 12, 300, 20)];//Change your size, you will need to experiment with the size

    [yourLabel setTextColor:[UIColor redColor]];
    [yourLabel setBackgroundColor:[UIColor clearColor]];
    [yourLabel setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]];
    [yourLabel setText:@""];
    [textField1 addSubview:yourLabel];
    textField1.delegate=self;
    [self textField:textField1 shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@""];//To change the limit label
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    int MAXLENGTH=20;

    NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if(newText.length>MAXLENGTH) {
        return NO;
    }
    [yourLabel setText:[NSString stringWithFormat:@"%lu",MAXLENGTH-newText.length]];

    return YES;
}

我希望这符合您的需求,让您开始。

不要忘记订阅UITextFieldDelegate。在.h文件中,声明:

@interface ViewController : UIViewController<UITextFieldDelegate>// -- < UITextFieldDelegate

答案 1 :(得分:5)

我创建了this UITextFieldLimit子类:

  • 支持多个文本字段
  • 设置文字长度限制
  • 预防粘贴
  • 在文本字段内显示左侧字符的标签,在停止编辑时隐藏。
  • 代表
  • 没有人物离开时动摇动画。

从此GitHub存储库中抓取UITextFieldLimit.hUITextFieldLimit.m

https://github.com/JonathanGurebo/UITextFieldLimit

开始测试!

标记您的故事板创建的UITextField并使用Identity Inspector将其链接到我的子类:

Identity Inspector

然后您可以将其链接到IBOutlet并设置限制(默认值为10)。


您的ViewController.h文件应包含:(如果您不想修改设置,例如限制)

#import "UITextFieldLimit.h"

/.../

@property (weak, nonatomic) IBOutlet UITextFieldLimit *textFieldLimit; // <--Your IBOutlet

您的ViewController.m文件应为@synthesize textFieldLimit


在ViewController.m文件中设置文本长度限制:

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    [textFieldLimit setLimit:25];// <-- and you won't be able to put more than 25 characters in the TextField.
}

希望全班同学帮助你。祝你好运!

答案 2 :(得分:1)

要求1和3直接使用委托方法:

// maxLength is a publicly assignable property

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // this covers paste, also
    NSString *candidateString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (candidateString.length > self.maxLength) return NO;

    [self updateCaptionSizeForText:candidateString];

    // requirement 2
    [self updateCharsRemainingLabel:candidateString.length];
    return YES;
}

要求2可以通过添加标签并更新其值来实现:

- (void)updateCharsRemainingLabel:(NSInteger)currentLength {
    NSInteger remaining = self.maxLength - currentLength;
    self.charsRemainingLabel.text = [NSString stringWithFormat:@"%d", remaining];
}
相关问题