diff --git a/tns-core-modules/ui/editable-text-base/editable-text-base-common.ts b/tns-core-modules/ui/editable-text-base/editable-text-base-common.ts index 0911ea533..0ea05c3e7 100644 --- a/tns-core-modules/ui/editable-text-base/editable-text-base-common.ts +++ b/tns-core-modules/ui/editable-text-base/editable-text-base-common.ts @@ -13,6 +13,7 @@ export abstract class EditableTextBase extends TextBase implements EditableTextB public editable: boolean; public autocorrect: boolean; public hint: string; + public maxLength: number; public abstract dismissSoftInput(); public abstract _setInputType(inputType: number): void; @@ -49,4 +50,6 @@ export const autocorrectProperty = new Property({ nam autocorrectProperty.register(EditableTextBase); export const hintProperty = new Property({ name: "hint", defaultValue: "" }); -hintProperty.register(EditableTextBase); \ No newline at end of file + +export const maxLengthProperty = new Property({ name: "maxLength", defaultValue: -1 }); +maxLengthProperty.register(EditableTextBase); diff --git a/tns-core-modules/ui/editable-text-base/editable-text-base.android.ts b/tns-core-modules/ui/editable-text-base/editable-text-base.android.ts index d1f946d89..aa6747ebc 100644 --- a/tns-core-modules/ui/editable-text-base/editable-text-base.android.ts +++ b/tns-core-modules/ui/editable-text-base/editable-text-base.android.ts @@ -2,7 +2,7 @@ EditableTextBase as EditableTextBaseCommon, keyboardTypeProperty, returnKeyTypeProperty, editableProperty, autocapitalizationTypeProperty, autocorrectProperty, hintProperty, - textProperty, placeholderColorProperty, Color, textTransformProperty + textProperty, placeholderColorProperty, Color, textTransformProperty, maxLengthProperty } from "./editable-text-base-common"; import { ad } from "../../utils/utils"; @@ -429,4 +429,8 @@ export abstract class EditableTextBase extends EditableTextBaseCommon { [textTransformProperty.setNative](value: "default") { // } -} \ No newline at end of file + + [maxLengthProperty.setNative](value: number) { + this.nativeView.setFilters([new android.text.InputFilter.LengthFilter(+value)]); + } +} diff --git a/tns-core-modules/ui/editable-text-base/editable-text-base.d.ts b/tns-core-modules/ui/editable-text-base/editable-text-base.d.ts index eaf6ad152..d4da01d11 100644 --- a/tns-core-modules/ui/editable-text-base/editable-text-base.d.ts +++ b/tns-core-modules/ui/editable-text-base/editable-text-base.d.ts @@ -45,6 +45,11 @@ export class EditableTextBase extends TextBase { */ hint: string; + /** + * Limits input to a certain number of characters. + */ + maxLength: number; + /** * Hides the soft input method, ususally a soft keyboard. */ @@ -71,6 +76,7 @@ export const autocapitalizationTypeProperty: Property; export const hintProperty: Property; export const placeholderColorProperty: CssProperty; +export const maxLengthProperty: Property; //@private /** diff --git a/tns-core-modules/ui/text-field/text-field.ios.ts b/tns-core-modules/ui/text-field/text-field.ios.ts index 76e24ac83..c2d13367f 100644 --- a/tns-core-modules/ui/text-field/text-field.ios.ts +++ b/tns-core-modules/ui/text-field/text-field.ios.ts @@ -1,5 +1,5 @@ import { - TextFieldBase, secureProperty, textProperty, hintProperty, colorProperty, placeholderColorProperty, + TextFieldBase, secureProperty, textProperty, hintProperty, colorProperty, placeholderColorProperty, maxLengthProperty, Length, paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty, _updateCharactersInRangeReplacementString, Color, layout } from "./text-field-common"; @@ -67,6 +67,10 @@ class UITextFieldDelegateImpl extends NSObject implements UITextFieldDelegate { public textFieldShouldChangeCharactersInRangeReplacementString(textField: UITextField, range: NSRange, replacementString: string): boolean { const owner = this._owner.get(); if (owner) { + if (owner.maxLength > -1 && owner.maxLength <= textField.text.length && replacementString.length > range.length) { + return false; + } + if (owner.updateTextTrigger === "textChanged") { if (textField.secureTextEntry && this.firstEdit) { textProperty.nativeValueChange(owner, replacementString); @@ -128,6 +132,7 @@ class UITextFieldImpl extends UITextField { export class TextField extends TextFieldBase { private _ios: UITextField; private _delegate: UITextFieldDelegateImpl; + private _maxLength: number = -1; nativeView: UITextField; constructor() { @@ -234,4 +239,11 @@ export class TextField extends TextFieldBase { [paddingLeftProperty.setNative](value: Length) { // Padding is realized via UITextFieldImpl.textRectForBounds method } + + [maxLengthProperty.getDefault](): number { + return this._maxLength; + } + [maxLengthProperty.setNative](value: number) { + this._maxLength = +value; + } } \ No newline at end of file diff --git a/tns-core-modules/ui/text-view/text-view.ios.ts b/tns-core-modules/ui/text-view/text-view.ios.ts index f2e22efa8..f2b4f3bb7 100644 --- a/tns-core-modules/ui/text-view/text-view.ios.ts +++ b/tns-core-modules/ui/text-view/text-view.ios.ts @@ -2,7 +2,7 @@ import { EditableTextBase, editableProperty, hintProperty, textProperty, colorProperty, placeholderColorProperty, borderTopWidthProperty, borderRightWidthProperty, borderBottomWidthProperty, borderLeftWidthProperty, - paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty, + paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty, maxLengthProperty, Length, _updateCharactersInRangeReplacementString, Color, layout } from "../editable-text-base"; @@ -51,8 +51,14 @@ class UITextViewDelegateImpl extends NSObject implements UITextViewDelegate { public textViewShouldChangeTextInRangeReplacementText(textView: UITextView, range: NSRange, replacementString: string): boolean { const owner = this._owner.get(); - if (owner && owner.formattedText) { - _updateCharactersInRangeReplacementString(owner.formattedText, range.location, range.length, replacementString); + if (owner) { + if (owner.maxLength > -1 && owner.maxLength <= textView.text.length && replacementString.length > range.length) { + return false; + } + + if (owner.formattedText) { + _updateCharactersInRangeReplacementString(owner.formattedText, range.location, range.length, replacementString); + } } return true; @@ -63,6 +69,7 @@ export class TextView extends EditableTextBase implements TextViewDefinition { private _ios: UITextView; private _delegate: UITextViewDelegateImpl; private _isShowingHint: boolean; + private _maxLength: number = -1; constructor() { super(); @@ -268,6 +275,13 @@ export class TextView extends EditableTextBase implements TextViewDefinition { let left = layout.toDeviceIndependentPixels(this.effectivePaddingLeft + this.effectiveBorderLeftWidth); this.nativeView.textContainerInset = { top: inset.top, left: left, bottom: inset.bottom, right: inset.right }; } + + [maxLengthProperty.getDefault](): number { + return this._maxLength; + } + [maxLengthProperty.setNative](value: number) { + this._maxLength = +value; + } } // TextView.prototype.recycleNativeView = true; \ No newline at end of file