Skip to content

Commit

Permalink
feat(core): maxLines support for all text components (#9884)
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug authored and NathanWalker committed Jul 9, 2022
1 parent fbd1e23 commit 7ff7233
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 40 deletions.
1 change: 0 additions & 1 deletion packages/core/application/index.android.ts
Expand Up @@ -365,7 +365,6 @@ function initLifecycleCallbacks() {
rootView.getViewTreeObserver().addOnGlobalLayoutListener(global.onGlobalLayoutListener);
});


let activitiesStarted = 0;

const lifecycleCallbacks = new android.app.Application.ActivityLifecycleCallbacks(<any>{
Expand Down
2 changes: 2 additions & 0 deletions packages/core/core-types/index.ts
Expand Up @@ -86,6 +86,8 @@ export namespace CoreTypes {
export const nowrap = 'nowrap';
}

export type MaxLinesType = number;

export type OrientationType = 'horizontal' | 'vertical';
export module Orientation {
export const horizontal = 'horizontal';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/ui/button/index.ios.ts
Expand Up @@ -224,7 +224,7 @@ export class Button extends ButtonBase {
switch (value) {
case 'normal':
nativeView.lineBreakMode = NSLineBreakMode.ByWordWrapping;
nativeView.numberOfLines = 0;
nativeView.numberOfLines = this.maxLines;
break;
case 'nowrap':
case 'initial':
Expand Down
2 changes: 1 addition & 1 deletion packages/core/ui/label/index.ios.ts
Expand Up @@ -117,7 +117,7 @@ export class Label extends TextBase implements LabelDefinition {
switch (value) {
case 'normal':
nativeView.lineBreakMode = NSLineBreakMode.ByWordWrapping;
nativeView.numberOfLines = 0;
nativeView.numberOfLines = this.maxLines;
break;
case 'nowrap':
case 'initial':
Expand Down
2 changes: 2 additions & 0 deletions packages/core/ui/styling/style/index.ts
Expand Up @@ -154,6 +154,8 @@ export class Style extends Observable implements StyleDefinition {
public fontWeight: FontWeight;
public font: string;

public maxLines: CoreTypes.MaxLinesType;

public androidElevation: number;
public androidDynamicElevationOffset: number;
public zIndex: number;
Expand Down
11 changes: 10 additions & 1 deletion packages/core/ui/text-base/index.android.ts
@@ -1,5 +1,5 @@
// Types
import { getClosestPropertyValue } from './text-base-common';
import { getClosestPropertyValue, maxLinesProperty } from './text-base-common';
import { CSSShadow } from '../styling/css-shadow';

// Requires
Expand Down Expand Up @@ -462,6 +462,15 @@ export class TextBase extends TextBaseCommon {
}
}

[maxLinesProperty.setNative](value: number) {
const nativeTextViewProtected = this.nativeTextViewProtected;
if (value <= 0) {
nativeTextViewProtected.setMaxLines(Number.MAX_SAFE_INTEGER);
} else {
nativeTextViewProtected.setMaxLines(typeof value === 'string' ? parseInt(value, 10) : value);
}
}

_setNativeText(reset = false): void {
if (reset) {
this.nativeTextViewProtected.setText(null);
Expand Down
5 changes: 5 additions & 0 deletions packages/core/ui/text-base/index.d.ts
Expand Up @@ -63,6 +63,11 @@ export class TextBase extends View implements AddChildFromBuilder {
*/
whiteSpace: CoreTypes.WhiteSpaceType;

/**
* Gets or sets white space style property.
*/
maxLines: CoreTypes.MaxLinesType;

/**
* Gets or sets padding style property.
*/
Expand Down
19 changes: 19 additions & 0 deletions packages/core/ui/text-base/index.ios.ts
Expand Up @@ -13,6 +13,7 @@ import { isString, isNullOrUndefined } from '../../utils/types';
import { iOSNativeHelper } from '../../utils';
import { Trace } from '../../trace';
import { CoreTypes } from '../../core-types';
import { maxLinesProperty } from './text-base-common';

export * from './text-base-common';

Expand Down Expand Up @@ -197,6 +198,24 @@ export class TextBase extends TextBaseCommon {
this._setShadow(value);
}

[maxLinesProperty.setNative](value: CoreTypes.MaxLinesType) {
const nativeTextViewProtected = this.nativeTextViewProtected;
const numberOfLines = this.whiteSpace === 'normal' ? value : 1;
if (nativeTextViewProtected instanceof UITextView) {
nativeTextViewProtected.textContainer.maximumNumberOfLines = numberOfLines;

if (value !== 0) {
nativeTextViewProtected.textContainer.lineBreakMode = NSLineBreakMode.ByTruncatingTail;
} else {
nativeTextViewProtected.textContainer.lineBreakMode = NSLineBreakMode.ByWordWrapping;
}
} else if (nativeTextViewProtected instanceof UILabel) {
nativeTextViewProtected.numberOfLines = numberOfLines;
} else if (nativeTextViewProtected instanceof UIButton) {
nativeTextViewProtected.titleLabel.numberOfLines = numberOfLines;
}
}

_setColor(color: UIColor): void {
if (this.nativeTextViewProtected instanceof UIButton) {
this.nativeTextViewProtected.setTitleColorForState(color, UIControlState.Normal);
Expand Down
14 changes: 14 additions & 0 deletions packages/core/ui/text-base/text-base-common.ts
Expand Up @@ -90,6 +90,13 @@ export abstract class TextBaseCommon extends View implements TextBaseDefinition
this.style.lineHeight = value;
}

get maxLines(): CoreTypes.MaxLinesType {
return this.style.maxLines;
}
set maxLines(value: CoreTypes.MaxLinesType) {
this.style.maxLines = value;
}

get textAlignment(): CoreTypes.TextAlignmentType {
return this.style.textAlignment;
}
Expand Down Expand Up @@ -310,4 +317,11 @@ export const lineHeightProperty = new InheritedCssProperty<Style, number>({
});
lineHeightProperty.register(Style);

export const maxLinesProperty = new CssProperty<Style, CoreTypes.MaxLinesType>({
name: 'maxLines',
cssName: 'max-lines',
valueConverter: (v) => (v === 'none' ? 0 : parseInt(v, 10)),
});
maxLinesProperty.register(Style);

export const resetSymbol = Symbol('textPropertyDefault');
16 changes: 1 addition & 15 deletions packages/core/ui/text-view/index.android.ts
@@ -1,4 +1,4 @@
import { TextViewBase as TextViewBaseCommon, maxLinesProperty } from './text-view-common';
import { TextViewBase as TextViewBaseCommon } from './text-view-common';
import { CSSType } from '../core/view';

export * from '../text-base';
Expand All @@ -15,20 +15,6 @@ export class TextView extends TextViewBaseCommon {
this.nativeTextViewProtected.setGravity(android.view.Gravity.TOP | android.view.Gravity.START);
}

[maxLinesProperty.getDefault](): number {
return 0;
}

[maxLinesProperty.setNative](value: number) {
if (value <= 0) {
this.nativeTextViewProtected.setMaxLines(Number.MAX_VALUE);

return;
}

this.nativeTextViewProtected.setMaxLines(value);
}

public _onReturnPress() {
this.notify({ eventName: TextView.returnPressEvent, object: this });
}
Expand Down
14 changes: 1 addition & 13 deletions packages/core/ui/text-view/index.ios.ts
@@ -1,6 +1,6 @@
import { ScrollEventData } from '../scroll-view';
import { textProperty } from '../text-base';
import { TextViewBase as TextViewBaseCommon, maxLinesProperty } from './text-view-common';
import { TextViewBase as TextViewBaseCommon } from './text-view-common';
import { editableProperty, hintProperty, placeholderColorProperty, _updateCharactersInRangeReplacementString } from '../editable-text-base';
import { CoreTypes } from '../../core-types';
import { CSSType } from '../core/view';
Expand Down Expand Up @@ -408,18 +408,6 @@ export class TextView extends TextViewBaseCommon {
right: inset.right,
};
}
[maxLinesProperty.getDefault](): number {
return 0;
}
[maxLinesProperty.setNative](value: number) {
this.nativeTextViewProtected.textContainer.maximumNumberOfLines = value;

if (value !== 0) {
this.nativeTextViewProtected.textContainer.lineBreakMode = NSLineBreakMode.ByTruncatingTail;
} else {
this.nativeTextViewProtected.textContainer.lineBreakMode = NSLineBreakMode.ByWordWrapping;
}
}
}

TextView.prototype.recycleNativeView = 'auto';
9 changes: 1 addition & 8 deletions packages/core/ui/text-view/text-view-common.ts
@@ -1,14 +1,7 @@
import { TextView as TextViewDefinition } from '.';
import { EditableTextBase } from '../editable-text-base';
import { Property } from '../core/properties';

export class TextViewBase extends EditableTextBase implements TextViewDefinition {
public static returnPressEvent = 'returnPress';
public maxLines: number;
}

export const maxLinesProperty = new Property<EditableTextBase, number>({
name: 'maxLines',
valueConverter: parseInt,
});
maxLinesProperty.register(EditableTextBase);
}

0 comments on commit 7ff7233

Please sign in to comment.