Skip to content

Feature request: TextField maxLength property support. #3614

Description

@tsonevn

Including new TextField property, which allows to specifying the maximum length of the text

Workaround
Android

textfieldloaded(args){
var textfield = args.object;
var legth = parseInt(this.maxvalue);
            var array = [];
            array[0] = new android.text.InputFilter.LengthFilter(legth);
            textfield .android.setFilters(array);
}

iOS

textfieldloaded(args){
var textfield = args.object;
var legth = parseInt(this.maxvalue);
           var uiTextView = textfield .ios;
           let tf = <any>this.textField;
           var newWeakRef = new WeakRef(uiTextView);
           let newDelegate = newUITextFieldDelegateImpl.initWithOriginalDelegate(tf._delegate, legth);
           uiTextView.delegate = newDelegate;
}

class newUITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate {

    public static ObjCProtocols = [UITextFieldDelegate];

    private _originalDelegate: UITextFieldDelegate;
    private firstEdit: boolean;
    private maxlegth:number

    public static initWithOriginalDelegate(originalDelegate: UITextFieldDelegate, maxl:number): newUITextFieldDelegateImpl {
        console.log("initWithOwner")

        let delegate = <newUITextFieldDelegateImpl>newUITextFieldDelegateImpl.new();
        delegate._originalDelegate = originalDelegate;
        delegate.maxlegth=maxl;
        return delegate;
    }

    public textFieldShouldBeginEditing(textField: UITextField): boolean {
        return this._originalDelegate.textFieldShouldBeginEditing(textField);
    }

    public textFieldDidEndEditing(textField: UITextField) {
        return this._originalDelegate.textFieldDidEndEditing(textField);
    }

    public textFieldShouldClear(textField: UITextField) {
    

        return this._originalDelegate.textFieldShouldClear(textField);
    }

    public textFieldShouldReturn(textField: UITextField): boolean {
        
        return this._originalDelegate.textFieldShouldReturn(textField);
    }

    public textFieldShouldChangeCharactersInRangeReplacementString(textField: UITextField, range: NSRange, replacementString: string): boolean {
        if ((this.maxlegth <= textField.text.length)&&(replacementString.length > range.length)) {
            return false;
        }

        return this._originalDelegate.textFieldShouldChangeCharactersInRangeReplacementString(textField, range, replacementString);
    }
    
}

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions