feat(core): maxLines support for all text components (#9884)

This commit is contained in:
farfromrefuge
2022-04-30 19:50:05 +02:00
committed by GitHub
parent 17a87a4a86
commit df10ceb491
12 changed files with 57 additions and 40 deletions

View File

@@ -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
@@ -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);

View File

@@ -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.
*/

View File

@@ -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';
@@ -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);

View File

@@ -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;
}
@@ -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');