Skip to content

Commit 974bdb1

Browse files
author
Surdu
committed
feat(textview): added maxLines property
1 parent 904590e commit 974bdb1

8 files changed

Lines changed: 56 additions & 2 deletions

nativescript-core/ui/editable-text-base/editable-text-base-common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export abstract class EditableTextBase extends TextBase implements EditableTextB
1616
public autocorrect: boolean;
1717
public hint: string;
1818
public maxLength: number;
19+
public maxLines: number;
1920

2021
public abstract dismissSoftInput();
2122
public abstract _setInputType(inputType: number): void;
@@ -67,3 +68,6 @@ hintProperty.register(EditableTextBase);
6768

6869
export const maxLengthProperty = new Property<EditableTextBase, number>({ name: "maxLength", defaultValue: Number.POSITIVE_INFINITY, valueConverter: parseInt });
6970
maxLengthProperty.register(EditableTextBase);
71+
72+
export const maxLinesProperty = new Property<EditableTextBase, number>({ name: "maxLines", valueConverter: parseInt });
73+
maxLinesProperty.register(EditableTextBase);

nativescript-core/ui/editable-text-base/editable-text-base.android.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
EditableTextBase as EditableTextBaseCommon, keyboardTypeProperty,
33
returnKeyTypeProperty, editableProperty,
44
autocapitalizationTypeProperty, autocorrectProperty, hintProperty, resetSymbol,
5-
textProperty, placeholderColorProperty, Color, textTransformProperty, maxLengthProperty
5+
textProperty, placeholderColorProperty, Color, textTransformProperty, maxLengthProperty, maxLinesProperty
66
} from "./editable-text-base-common";
77

88
import { ad } from "../../utils/utils";
@@ -456,4 +456,12 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
456456
this.nativeTextViewProtected.setFilters(newFilters);
457457
}
458458
}
459+
460+
[maxLinesProperty.getDefault](): number {
461+
return -1;
462+
}
463+
464+
[maxLinesProperty.setNative](value: number) {
465+
this.nativeTextViewProtected.setMaxLines(value);
466+
}
459467
}

nativescript-core/ui/editable-text-base/editable-text-base.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ export class EditableTextBase extends TextBase {
5252
*/
5353
maxLength: number;
5454

55+
/**
56+
* Limits input to a certain number of lines.
57+
*/
58+
maxLines: number;
59+
5560
/**
5661
* Hides the soft input method, ususally a soft keyboard.
5762
*/
@@ -79,6 +84,7 @@ export const autocorrectProperty: Property<EditableTextBase, boolean>;
7984
export const hintProperty: Property<EditableTextBase, string>;
8085
export const placeholderColorProperty: CssProperty<Style, Color>;
8186
export const maxLengthProperty: Property<EditableTextBase, number>;
87+
export const maxLinesProperty: Property<EditableTextBase, number>;
8288

8389
//@private
8490
/**

nativescript-core/ui/editable-text-base/editable-text-base.ios.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
EditableTextBase as EditableTextBaseCommon, keyboardTypeProperty,
33
returnKeyTypeProperty,
4-
autocapitalizationTypeProperty, autocorrectProperty, FormattedString
4+
autocapitalizationTypeProperty, autocorrectProperty, FormattedString, maxLinesProperty
55
} from "./editable-text-base-common";
66

77
export * from "./editable-text-base-common";
@@ -186,6 +186,21 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
186186

187187
this.nativeTextViewProtected.autocorrectionType = newValue;
188188
}
189+
190+
[maxLinesProperty.getDefault](): number {
191+
return 0;
192+
}
193+
194+
[maxLinesProperty.setNative](value: number) {
195+
this.nativeTextViewProtected.textContainer.maximumNumberOfLines = value;
196+
197+
if (value !== 0) {
198+
this.nativeTextViewProtected.textContainer.lineBreakMode = NSLineBreakMode.ByTruncatingTail;
199+
}
200+
else {
201+
this.nativeTextViewProtected.textContainer.lineBreakMode = NSLineBreakMode.ByWordWrapping;
202+
}
203+
}
189204
}
190205

191206
export function _updateCharactersInRangeReplacementString(formattedText: FormattedString, rangeLocation: number, rangeLength: number, replacementString: string): void {

tests/app/ui/text-view/text-view-tests-native.android.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ export function typeTextNatively(textView: textViewModule.TextView, text: string
6363
textView.android.setText(text);
6464
textView.android.clearFocus();
6565
}
66+
67+
export function getNativeMaxLines(textView: textViewModule.TextView): number {
68+
return textView.android.getMaxLines();
69+
}

tests/app/ui/text-view/text-view-tests-native.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export declare function getNativeColor(textView: textViewModule.TextView): color
1010
export declare function getNativeBackgroundColor(textView: textViewModule.TextView): colorModule.Color;
1111
export declare function getNativeTextAlignment(textView: textViewModule.TextView): string;
1212
export declare function typeTextNatively(textView: textViewModule.TextView, text: string): void;
13+
export declare function getNativeMaxLines(textView: textViewModule.TextView): number;

tests/app/ui/text-view/text-view-tests-native.ios.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@ export function typeTextNatively(textView: textViewModule.TextView, text: string
5151
// Setting the text will not trigger the delegate method, so we have to do it by hand.
5252
textView.ios.delegate.textViewDidEndEditing(textView.ios);
5353
}
54+
55+
export function getNativeMaxLines(textView: textViewModule.TextView): number {
56+
return textView.ios.textContainer.maximumNumberOfLines;
57+
}

tests/app/ui/text-view/text-view-tests.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,18 @@ export var testBindEditableToBindingConext = function () {
346346
});
347347
};
348348

349+
export var testSetMaxLines = function () {
350+
helper.buildUIAndRunTest(_createTextViewFunc(), function (views: Array<viewModule.View>) {
351+
var textView = <textViewModule.TextView>views[0];
352+
353+
textView.maxLines = 3;
354+
355+
var expectedValue = 3;
356+
var actualValue = textViewTestsNative.getNativeMaxLines(textView);
357+
TKUnit.assert(actualValue === expectedValue, "Actual: " + actualValue + "; Expected: " + expectedValue);
358+
});
359+
};
360+
349361
var expectedFontSize = 42;
350362
export var testLocalFontSizeFromCss = function () {
351363
helper.buildUIAndRunTest(_createTextViewFunc(), function (views: Array<viewModule.View>) {

0 commit comments

Comments
 (0)