mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: format
This commit is contained in:
@@ -1,16 +1,42 @@
|
||||
// Types
|
||||
import { TextDecoration, TextAlignment, TextTransform, WhiteSpace, getClosestPropertyValue } from "./text-base-common";
|
||||
import {
|
||||
TextDecoration,
|
||||
TextAlignment,
|
||||
TextTransform,
|
||||
WhiteSpace,
|
||||
getClosestPropertyValue,
|
||||
} from "./text-base-common";
|
||||
|
||||
// Requires
|
||||
import { Font } from "../styling/font";
|
||||
import { backgroundColorProperty, VerticalAlignment } from "../styling/style-properties";
|
||||
import {
|
||||
TextBaseCommon, formattedTextProperty, textAlignmentProperty, textDecorationProperty,
|
||||
textProperty, textTransformProperty, letterSpacingProperty,
|
||||
whiteSpaceProperty, lineHeightProperty, isBold, resetSymbol
|
||||
backgroundColorProperty,
|
||||
VerticalAlignment,
|
||||
} from "../styling/style-properties";
|
||||
import {
|
||||
TextBaseCommon,
|
||||
formattedTextProperty,
|
||||
textAlignmentProperty,
|
||||
textDecorationProperty,
|
||||
textProperty,
|
||||
textTransformProperty,
|
||||
letterSpacingProperty,
|
||||
whiteSpaceProperty,
|
||||
lineHeightProperty,
|
||||
isBold,
|
||||
resetSymbol,
|
||||
} from "./text-base-common";
|
||||
import { Color } from "../../color";
|
||||
import { colorProperty, fontSizeProperty, fontInternalProperty, paddingLeftProperty, paddingTopProperty, paddingRightProperty, paddingBottomProperty, Length } from "../styling/style-properties";
|
||||
import {
|
||||
colorProperty,
|
||||
fontSizeProperty,
|
||||
fontInternalProperty,
|
||||
paddingLeftProperty,
|
||||
paddingTopProperty,
|
||||
paddingRightProperty,
|
||||
paddingBottomProperty,
|
||||
Length,
|
||||
} from "../styling/style-properties";
|
||||
import { FormattedString } from "./formatted-string";
|
||||
import { Span } from "./span";
|
||||
import { layout } from "../../utils/utils";
|
||||
@@ -21,78 +47,91 @@ export * from "./text-base-common";
|
||||
let TextTransformation: TextTransformation;
|
||||
|
||||
export interface TextTransformation {
|
||||
new(owner: TextBase): any /* android.text.method.TransformationMethod */;
|
||||
new (owner: TextBase): any /* android.text.method.TransformationMethod */;
|
||||
}
|
||||
|
||||
function initializeTextTransformation(): void {
|
||||
if (TextTransformation) {
|
||||
return;
|
||||
}
|
||||
if (TextTransformation) {
|
||||
return;
|
||||
}
|
||||
|
||||
@Interfaces([android.text.method.TransformationMethod])
|
||||
class TextTransformationImpl extends java.lang.Object implements android.text.method.TransformationMethod {
|
||||
constructor(public textBase: TextBase) {
|
||||
super();
|
||||
@Interfaces([android.text.method.TransformationMethod])
|
||||
class TextTransformationImpl extends java.lang.Object
|
||||
implements android.text.method.TransformationMethod {
|
||||
constructor(public textBase: TextBase) {
|
||||
super();
|
||||
|
||||
return global.__native(this);
|
||||
}
|
||||
return global.__native(this);
|
||||
}
|
||||
|
||||
public getTransformation(charSeq: any, view: android.view.View): any {
|
||||
// NOTE: Do we need to transform the new text here?
|
||||
const formattedText = this.textBase.formattedText;
|
||||
if (formattedText) {
|
||||
return createSpannableStringBuilder(formattedText, (<android.widget.TextView>view).getTextSize());
|
||||
}
|
||||
else {
|
||||
const text = this.textBase.text;
|
||||
const stringValue = (text === null || text === undefined) ? "" : text.toString();
|
||||
public getTransformation(charSeq: any, view: android.view.View): any {
|
||||
// NOTE: Do we need to transform the new text here?
|
||||
const formattedText = this.textBase.formattedText;
|
||||
if (formattedText) {
|
||||
return createSpannableStringBuilder(
|
||||
formattedText,
|
||||
(<android.widget.TextView>view).getTextSize()
|
||||
);
|
||||
} else {
|
||||
const text = this.textBase.text;
|
||||
const stringValue =
|
||||
text === null || text === undefined ? "" : text.toString();
|
||||
|
||||
return getTransformedText(stringValue, this.textBase.textTransform);
|
||||
}
|
||||
}
|
||||
return getTransformedText(
|
||||
stringValue,
|
||||
this.textBase.textTransform
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public onFocusChanged(view: android.view.View, sourceText: string, focused: boolean, direction: number, previouslyFocusedRect: android.graphics.Rect): void {
|
||||
// Do nothing for now.
|
||||
}
|
||||
}
|
||||
public onFocusChanged(
|
||||
view: android.view.View,
|
||||
sourceText: string,
|
||||
focused: boolean,
|
||||
direction: number,
|
||||
previouslyFocusedRect: android.graphics.Rect
|
||||
): void {
|
||||
// Do nothing for now.
|
||||
}
|
||||
}
|
||||
|
||||
TextTransformation = TextTransformationImpl;
|
||||
TextTransformation = TextTransformationImpl;
|
||||
}
|
||||
|
||||
interface ClickableSpan {
|
||||
new (owner: Span): android.text.style.ClickableSpan;
|
||||
new (owner: Span): android.text.style.ClickableSpan;
|
||||
}
|
||||
|
||||
let ClickableSpan: ClickableSpan;
|
||||
|
||||
function initializeClickableSpan(): void {
|
||||
if (ClickableSpan) {
|
||||
return;
|
||||
}
|
||||
if (ClickableSpan) {
|
||||
return;
|
||||
}
|
||||
|
||||
class ClickableSpanImpl extends android.text.style.ClickableSpan {
|
||||
owner: WeakRef<Span>;
|
||||
class ClickableSpanImpl extends android.text.style.ClickableSpan {
|
||||
owner: WeakRef<Span>;
|
||||
|
||||
constructor(owner: Span) {
|
||||
super();
|
||||
this.owner = new WeakRef(owner);
|
||||
constructor(owner: Span) {
|
||||
super();
|
||||
this.owner = new WeakRef(owner);
|
||||
|
||||
return global.__native(this);
|
||||
}
|
||||
onClick(view: android.view.View): void {
|
||||
const owner = this.owner.get();
|
||||
if (owner) {
|
||||
owner._emit(Span.linkTapEvent);
|
||||
}
|
||||
view.clearFocus();
|
||||
view.invalidate();
|
||||
}
|
||||
updateDrawState(tp: android.text.TextPaint): void {
|
||||
// don't style as link
|
||||
}
|
||||
}
|
||||
return global.__native(this);
|
||||
}
|
||||
onClick(view: android.view.View): void {
|
||||
const owner = this.owner.get();
|
||||
if (owner) {
|
||||
owner._emit(Span.linkTapEvent);
|
||||
}
|
||||
view.clearFocus();
|
||||
view.invalidate();
|
||||
}
|
||||
updateDrawState(tp: android.text.TextPaint): void {
|
||||
// don't style as link
|
||||
}
|
||||
}
|
||||
|
||||
ClickableSpan = ClickableSpanImpl;
|
||||
ClickableSpan = ClickableSpanImpl;
|
||||
}
|
||||
|
||||
interface BaselineAdjustedSpan {
|
||||
@@ -165,358 +204,460 @@ function initializeBaselineAdjustedSpan(): void {
|
||||
}
|
||||
|
||||
export class TextBase extends TextBaseCommon {
|
||||
nativeViewProtected: android.widget.TextView;
|
||||
nativeTextViewProtected: android.widget.TextView;
|
||||
private _defaultTransformationMethod: android.text.method.TransformationMethod;
|
||||
private _paintFlags: number;
|
||||
private _minHeight: number;
|
||||
private _maxHeight: number;
|
||||
private _minLines: number;
|
||||
private _maxLines: number;
|
||||
private _tappable: boolean = false;
|
||||
private _defaultMovementMethod: android.text.method.MovementMethod;
|
||||
nativeViewProtected: android.widget.TextView;
|
||||
nativeTextViewProtected: android.widget.TextView;
|
||||
private _defaultTransformationMethod: android.text.method.TransformationMethod;
|
||||
private _paintFlags: number;
|
||||
private _minHeight: number;
|
||||
private _maxHeight: number;
|
||||
private _minLines: number;
|
||||
private _maxLines: number;
|
||||
private _tappable: boolean = false;
|
||||
private _defaultMovementMethod: android.text.method.MovementMethod;
|
||||
|
||||
public initNativeView(): void {
|
||||
super.initNativeView();
|
||||
initializeTextTransformation();
|
||||
const nativeView = this.nativeTextViewProtected;
|
||||
this._defaultTransformationMethod = nativeView.getTransformationMethod();
|
||||
this._defaultMovementMethod = nativeView.getMovementMethod();
|
||||
this._minHeight = nativeView.getMinHeight();
|
||||
this._maxHeight = nativeView.getMaxHeight();
|
||||
this._minLines = nativeView.getMinLines();
|
||||
this._maxLines = nativeView.getMaxLines();
|
||||
}
|
||||
public initNativeView(): void {
|
||||
super.initNativeView();
|
||||
initializeTextTransformation();
|
||||
const nativeView = this.nativeTextViewProtected;
|
||||
this._defaultTransformationMethod = nativeView.getTransformationMethod();
|
||||
this._defaultMovementMethod = nativeView.getMovementMethod();
|
||||
this._minHeight = nativeView.getMinHeight();
|
||||
this._maxHeight = nativeView.getMaxHeight();
|
||||
this._minLines = nativeView.getMinLines();
|
||||
this._maxLines = nativeView.getMaxLines();
|
||||
}
|
||||
|
||||
public resetNativeView(): void {
|
||||
super.resetNativeView();
|
||||
const nativeView = this.nativeTextViewProtected;
|
||||
// We reset it here too because this could be changed by multiple properties - whiteSpace, secure, textTransform
|
||||
nativeView.setSingleLine(this._isSingleLine);
|
||||
nativeView.setTransformationMethod(this._defaultTransformationMethod);
|
||||
this._defaultTransformationMethod = null;
|
||||
public resetNativeView(): void {
|
||||
super.resetNativeView();
|
||||
const nativeView = this.nativeTextViewProtected;
|
||||
// We reset it here too because this could be changed by multiple properties - whiteSpace, secure, textTransform
|
||||
nativeView.setSingleLine(this._isSingleLine);
|
||||
nativeView.setTransformationMethod(this._defaultTransformationMethod);
|
||||
this._defaultTransformationMethod = null;
|
||||
|
||||
if (this._paintFlags !== undefined) {
|
||||
nativeView.setPaintFlags(this._paintFlags);
|
||||
this._paintFlags = undefined;
|
||||
}
|
||||
if (this._paintFlags !== undefined) {
|
||||
nativeView.setPaintFlags(this._paintFlags);
|
||||
this._paintFlags = undefined;
|
||||
}
|
||||
|
||||
if (this._minLines !== -1) {
|
||||
nativeView.setMinLines(this._minLines);
|
||||
} else {
|
||||
nativeView.setMinHeight(this._minHeight);
|
||||
}
|
||||
if (this._minLines !== -1) {
|
||||
nativeView.setMinLines(this._minLines);
|
||||
} else {
|
||||
nativeView.setMinHeight(this._minHeight);
|
||||
}
|
||||
|
||||
this._minHeight = this._minLines = undefined;
|
||||
this._minHeight = this._minLines = undefined;
|
||||
|
||||
if (this._maxLines !== -1) {
|
||||
nativeView.setMaxLines(this._maxLines);
|
||||
} else {
|
||||
nativeView.setMaxHeight(this._maxHeight);
|
||||
}
|
||||
if (this._maxLines !== -1) {
|
||||
nativeView.setMaxLines(this._maxLines);
|
||||
} else {
|
||||
nativeView.setMaxHeight(this._maxHeight);
|
||||
}
|
||||
|
||||
this._maxHeight = this._maxLines = undefined;
|
||||
}
|
||||
this._maxHeight = this._maxLines = undefined;
|
||||
}
|
||||
|
||||
[textProperty.getDefault](): symbol | number {
|
||||
return resetSymbol;
|
||||
}
|
||||
[textProperty.getDefault](): symbol | number {
|
||||
return resetSymbol;
|
||||
}
|
||||
|
||||
[textProperty.setNative](value: string | number | symbol) {
|
||||
const reset = value === resetSymbol;
|
||||
if (!reset && this.formattedText) {
|
||||
return;
|
||||
}
|
||||
[textProperty.setNative](value: string | number | symbol) {
|
||||
const reset = value === resetSymbol;
|
||||
if (!reset && this.formattedText) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._setTappableState(false);
|
||||
this._setTappableState(false);
|
||||
|
||||
this._setNativeText(reset);
|
||||
}
|
||||
this._setNativeText(reset);
|
||||
}
|
||||
|
||||
[formattedTextProperty.setNative](value: FormattedString) {
|
||||
const nativeView = this.nativeTextViewProtected;
|
||||
if (!value) {
|
||||
if (nativeView instanceof android.widget.Button &&
|
||||
nativeView.getTransformationMethod() instanceof TextTransformation) {
|
||||
nativeView.setTransformationMethod(this._defaultTransformationMethod);
|
||||
}
|
||||
}
|
||||
[formattedTextProperty.setNative](value: FormattedString) {
|
||||
const nativeView = this.nativeTextViewProtected;
|
||||
if (!value) {
|
||||
if (
|
||||
nativeView instanceof android.widget.Button &&
|
||||
nativeView.getTransformationMethod() instanceof
|
||||
TextTransformation
|
||||
) {
|
||||
nativeView.setTransformationMethod(
|
||||
this._defaultTransformationMethod
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Don't change the transformation method if this is secure TextField or we'll lose the hiding characters.
|
||||
if ((<any>this).secure) {
|
||||
return;
|
||||
}
|
||||
// Don't change the transformation method if this is secure TextField or we'll lose the hiding characters.
|
||||
if ((<any>this).secure) {
|
||||
return;
|
||||
}
|
||||
|
||||
const spannableStringBuilder = createSpannableStringBuilder(value, this.style.fontSize);
|
||||
nativeView.setText(<any>spannableStringBuilder);
|
||||
this._setTappableState(isStringTappable(value));
|
||||
const spannableStringBuilder = createSpannableStringBuilder(
|
||||
value,
|
||||
this.style.fontSize
|
||||
);
|
||||
nativeView.setText(<any>spannableStringBuilder);
|
||||
this._setTappableState(isStringTappable(value));
|
||||
|
||||
textProperty.nativeValueChange(this, (value === null || value === undefined) ? "" : value.toString());
|
||||
textProperty.nativeValueChange(
|
||||
this,
|
||||
value === null || value === undefined ? "" : value.toString()
|
||||
);
|
||||
|
||||
if (spannableStringBuilder && nativeView instanceof android.widget.Button &&
|
||||
!(nativeView.getTransformationMethod() instanceof TextTransformation)) {
|
||||
// Replace Android Button's default transformation (in case the developer has not already specified a text-transform) method
|
||||
// with our transformation method which can handle formatted text.
|
||||
// Otherwise, the default tranformation method of the Android Button will overwrite and ignore our spannableStringBuilder.
|
||||
nativeView.setTransformationMethod(new TextTransformation(this));
|
||||
}
|
||||
}
|
||||
if (
|
||||
spannableStringBuilder &&
|
||||
nativeView instanceof android.widget.Button &&
|
||||
!(
|
||||
nativeView.getTransformationMethod() instanceof
|
||||
TextTransformation
|
||||
)
|
||||
) {
|
||||
// Replace Android Button's default transformation (in case the developer has not already specified a text-transform) method
|
||||
// with our transformation method which can handle formatted text.
|
||||
// Otherwise, the default tranformation method of the Android Button will overwrite and ignore our spannableStringBuilder.
|
||||
nativeView.setTransformationMethod(new TextTransformation(this));
|
||||
}
|
||||
}
|
||||
|
||||
[textTransformProperty.setNative](value: TextTransform) {
|
||||
if (value === "initial") {
|
||||
this.nativeTextViewProtected.setTransformationMethod(this._defaultTransformationMethod);
|
||||
[textTransformProperty.setNative](value: TextTransform) {
|
||||
if (value === "initial") {
|
||||
this.nativeTextViewProtected.setTransformationMethod(
|
||||
this._defaultTransformationMethod
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't change the transformation method if this is secure TextField or we'll lose the hiding characters.
|
||||
if ((<any>this).secure) {
|
||||
return;
|
||||
}
|
||||
// Don't change the transformation method if this is secure TextField or we'll lose the hiding characters.
|
||||
if ((<any>this).secure) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.nativeTextViewProtected.setTransformationMethod(new TextTransformation(this));
|
||||
}
|
||||
this.nativeTextViewProtected.setTransformationMethod(
|
||||
new TextTransformation(this)
|
||||
);
|
||||
}
|
||||
|
||||
[textAlignmentProperty.getDefault](): TextAlignment {
|
||||
return "initial";
|
||||
}
|
||||
[textAlignmentProperty.setNative](value: TextAlignment) {
|
||||
let verticalGravity = this.nativeTextViewProtected.getGravity() & android.view.Gravity.VERTICAL_GRAVITY_MASK;
|
||||
switch (value) {
|
||||
case "initial":
|
||||
case "left":
|
||||
this.nativeTextViewProtected.setGravity(android.view.Gravity.START | verticalGravity);
|
||||
break;
|
||||
[textAlignmentProperty.getDefault](): TextAlignment {
|
||||
return "initial";
|
||||
}
|
||||
[textAlignmentProperty.setNative](value: TextAlignment) {
|
||||
let verticalGravity =
|
||||
this.nativeTextViewProtected.getGravity() &
|
||||
android.view.Gravity.VERTICAL_GRAVITY_MASK;
|
||||
switch (value) {
|
||||
case "initial":
|
||||
case "left":
|
||||
this.nativeTextViewProtected.setGravity(
|
||||
android.view.Gravity.START | verticalGravity
|
||||
);
|
||||
break;
|
||||
|
||||
case "center":
|
||||
this.nativeTextViewProtected.setGravity(android.view.Gravity.CENTER_HORIZONTAL | verticalGravity);
|
||||
break;
|
||||
case "center":
|
||||
this.nativeTextViewProtected.setGravity(
|
||||
android.view.Gravity.CENTER_HORIZONTAL | verticalGravity
|
||||
);
|
||||
break;
|
||||
|
||||
case "right":
|
||||
this.nativeTextViewProtected.setGravity(android.view.Gravity.END | verticalGravity);
|
||||
break;
|
||||
}
|
||||
}
|
||||
case "right":
|
||||
this.nativeTextViewProtected.setGravity(
|
||||
android.view.Gravity.END | verticalGravity
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Overridden in TextField because setSingleLine(false) will remove methodTransformation.
|
||||
// and we don't want to allow TextField to be multiline
|
||||
[whiteSpaceProperty.setNative](value: WhiteSpace) {
|
||||
const nativeView = this.nativeTextViewProtected;
|
||||
switch (value) {
|
||||
case "initial":
|
||||
case "normal":
|
||||
nativeView.setSingleLine(false);
|
||||
nativeView.setEllipsize(null);
|
||||
break;
|
||||
case "nowrap":
|
||||
nativeView.setSingleLine(true);
|
||||
nativeView.setEllipsize(android.text.TextUtils.TruncateAt.END);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Overridden in TextField because setSingleLine(false) will remove methodTransformation.
|
||||
// and we don't want to allow TextField to be multiline
|
||||
[whiteSpaceProperty.setNative](value: WhiteSpace) {
|
||||
const nativeView = this.nativeTextViewProtected;
|
||||
switch (value) {
|
||||
case "initial":
|
||||
case "normal":
|
||||
nativeView.setSingleLine(false);
|
||||
nativeView.setEllipsize(null);
|
||||
break;
|
||||
case "nowrap":
|
||||
nativeView.setSingleLine(true);
|
||||
nativeView.setEllipsize(android.text.TextUtils.TruncateAt.END);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
[colorProperty.getDefault](): android.content.res.ColorStateList {
|
||||
return this.nativeTextViewProtected.getTextColors();
|
||||
}
|
||||
[colorProperty.setNative](value: Color | android.content.res.ColorStateList) {
|
||||
if (!this.formattedText || !(value instanceof Color)) {
|
||||
if (value instanceof Color) {
|
||||
this.nativeTextViewProtected.setTextColor(value.android);
|
||||
} else {
|
||||
this.nativeTextViewProtected.setTextColor(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
[colorProperty.getDefault](): android.content.res.ColorStateList {
|
||||
return this.nativeTextViewProtected.getTextColors();
|
||||
}
|
||||
[colorProperty.setNative](
|
||||
value: Color | android.content.res.ColorStateList
|
||||
) {
|
||||
if (!this.formattedText || !(value instanceof Color)) {
|
||||
if (value instanceof Color) {
|
||||
this.nativeTextViewProtected.setTextColor(value.android);
|
||||
} else {
|
||||
this.nativeTextViewProtected.setTextColor(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[fontSizeProperty.getDefault](): { nativeSize: number } {
|
||||
return { nativeSize: this.nativeTextViewProtected.getTextSize() };
|
||||
}
|
||||
[fontSizeProperty.setNative](value: number | { nativeSize: number }) {
|
||||
if (!this.formattedText || (typeof value !== "number")) {
|
||||
if (typeof value === "number") {
|
||||
this.nativeTextViewProtected.setTextSize(value);
|
||||
} else {
|
||||
this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, value.nativeSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
[fontSizeProperty.getDefault](): { nativeSize: number } {
|
||||
return { nativeSize: this.nativeTextViewProtected.getTextSize() };
|
||||
}
|
||||
[fontSizeProperty.setNative](value: number | { nativeSize: number }) {
|
||||
if (!this.formattedText || typeof value !== "number") {
|
||||
if (typeof value === "number") {
|
||||
this.nativeTextViewProtected.setTextSize(value);
|
||||
} else {
|
||||
this.nativeTextViewProtected.setTextSize(
|
||||
android.util.TypedValue.COMPLEX_UNIT_PX,
|
||||
value.nativeSize
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[lineHeightProperty.getDefault](): number {
|
||||
return this.nativeTextViewProtected.getLineHeight() / layout.getDisplayDensity();
|
||||
}
|
||||
[lineHeightProperty.setNative](value: number) {
|
||||
const fontHeight = this.nativeTextViewProtected.getPaint().getFontMetricsInt(null);
|
||||
this.nativeTextViewProtected.setLineSpacing(Math.max(value - fontHeight, 0) * layout.getDisplayDensity(), 1);
|
||||
}
|
||||
[lineHeightProperty.getDefault](): number {
|
||||
return (
|
||||
this.nativeTextViewProtected.getLineHeight() /
|
||||
layout.getDisplayDensity()
|
||||
);
|
||||
}
|
||||
[lineHeightProperty.setNative](value: number) {
|
||||
const fontHeight = this.nativeTextViewProtected
|
||||
.getPaint()
|
||||
.getFontMetricsInt(null);
|
||||
this.nativeTextViewProtected.setLineSpacing(
|
||||
Math.max(value - fontHeight, 0) * layout.getDisplayDensity(),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
[fontInternalProperty.getDefault](): android.graphics.Typeface {
|
||||
return this.nativeTextViewProtected.getTypeface();
|
||||
}
|
||||
[fontInternalProperty.setNative](value: Font | android.graphics.Typeface) {
|
||||
if (!this.formattedText || !(value instanceof Font)) {
|
||||
this.nativeTextViewProtected.setTypeface(value instanceof Font ? value.getAndroidTypeface() : value);
|
||||
}
|
||||
}
|
||||
[fontInternalProperty.getDefault](): android.graphics.Typeface {
|
||||
return this.nativeTextViewProtected.getTypeface();
|
||||
}
|
||||
[fontInternalProperty.setNative](value: Font | android.graphics.Typeface) {
|
||||
if (!this.formattedText || !(value instanceof Font)) {
|
||||
this.nativeTextViewProtected.setTypeface(
|
||||
value instanceof Font ? value.getAndroidTypeface() : value
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
[textDecorationProperty.getDefault](value: number) {
|
||||
return this._paintFlags = this.nativeTextViewProtected.getPaintFlags();
|
||||
}
|
||||
[textDecorationProperty.getDefault](value: number) {
|
||||
return (this._paintFlags = this.nativeTextViewProtected.getPaintFlags());
|
||||
}
|
||||
|
||||
[textDecorationProperty.setNative](value: number | TextDecoration) {
|
||||
switch (value) {
|
||||
case "none":
|
||||
this.nativeTextViewProtected.setPaintFlags(0);
|
||||
break;
|
||||
case "underline":
|
||||
this.nativeTextViewProtected.setPaintFlags(android.graphics.Paint.UNDERLINE_TEXT_FLAG);
|
||||
break;
|
||||
case "line-through":
|
||||
this.nativeTextViewProtected.setPaintFlags(android.graphics.Paint.STRIKE_THRU_TEXT_FLAG);
|
||||
break;
|
||||
case "underline line-through":
|
||||
this.nativeTextViewProtected.setPaintFlags(android.graphics.Paint.UNDERLINE_TEXT_FLAG | android.graphics.Paint.STRIKE_THRU_TEXT_FLAG);
|
||||
break;
|
||||
default:
|
||||
this.nativeTextViewProtected.setPaintFlags(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
[textDecorationProperty.setNative](value: number | TextDecoration) {
|
||||
switch (value) {
|
||||
case "none":
|
||||
this.nativeTextViewProtected.setPaintFlags(0);
|
||||
break;
|
||||
case "underline":
|
||||
this.nativeTextViewProtected.setPaintFlags(
|
||||
android.graphics.Paint.UNDERLINE_TEXT_FLAG
|
||||
);
|
||||
break;
|
||||
case "line-through":
|
||||
this.nativeTextViewProtected.setPaintFlags(
|
||||
android.graphics.Paint.STRIKE_THRU_TEXT_FLAG
|
||||
);
|
||||
break;
|
||||
case "underline line-through":
|
||||
this.nativeTextViewProtected.setPaintFlags(
|
||||
android.graphics.Paint.UNDERLINE_TEXT_FLAG |
|
||||
android.graphics.Paint.STRIKE_THRU_TEXT_FLAG
|
||||
);
|
||||
break;
|
||||
default:
|
||||
this.nativeTextViewProtected.setPaintFlags(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
[letterSpacingProperty.getDefault](): number {
|
||||
return org.nativescript.widgets.ViewHelper.getLetterspacing(this.nativeTextViewProtected);
|
||||
}
|
||||
[letterSpacingProperty.setNative](value: number) {
|
||||
org.nativescript.widgets.ViewHelper.setLetterspacing(this.nativeTextViewProtected, value);
|
||||
}
|
||||
[letterSpacingProperty.getDefault](): number {
|
||||
return org.nativescript.widgets.ViewHelper.getLetterspacing(
|
||||
this.nativeTextViewProtected
|
||||
);
|
||||
}
|
||||
[letterSpacingProperty.setNative](value: number) {
|
||||
org.nativescript.widgets.ViewHelper.setLetterspacing(
|
||||
this.nativeTextViewProtected,
|
||||
value
|
||||
);
|
||||
}
|
||||
|
||||
[paddingTopProperty.getDefault](): Length {
|
||||
return { value: this._defaultPaddingTop, unit: "px" };
|
||||
}
|
||||
[paddingTopProperty.setNative](value: Length) {
|
||||
org.nativescript.widgets.ViewHelper.setPaddingTop(this.nativeTextViewProtected, Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderTopWidth, 0));
|
||||
}
|
||||
[paddingTopProperty.getDefault](): Length {
|
||||
return { value: this._defaultPaddingTop, unit: "px" };
|
||||
}
|
||||
[paddingTopProperty.setNative](value: Length) {
|
||||
org.nativescript.widgets.ViewHelper.setPaddingTop(
|
||||
this.nativeTextViewProtected,
|
||||
Length.toDevicePixels(value, 0) +
|
||||
Length.toDevicePixels(this.style.borderTopWidth, 0)
|
||||
);
|
||||
}
|
||||
|
||||
[paddingRightProperty.getDefault](): Length {
|
||||
return { value: this._defaultPaddingRight, unit: "px" };
|
||||
}
|
||||
[paddingRightProperty.setNative](value: Length) {
|
||||
org.nativescript.widgets.ViewHelper.setPaddingRight(this.nativeTextViewProtected, Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderRightWidth, 0));
|
||||
}
|
||||
[paddingRightProperty.getDefault](): Length {
|
||||
return { value: this._defaultPaddingRight, unit: "px" };
|
||||
}
|
||||
[paddingRightProperty.setNative](value: Length) {
|
||||
org.nativescript.widgets.ViewHelper.setPaddingRight(
|
||||
this.nativeTextViewProtected,
|
||||
Length.toDevicePixels(value, 0) +
|
||||
Length.toDevicePixels(this.style.borderRightWidth, 0)
|
||||
);
|
||||
}
|
||||
|
||||
[paddingBottomProperty.getDefault](): Length {
|
||||
return { value: this._defaultPaddingBottom, unit: "px" };
|
||||
}
|
||||
[paddingBottomProperty.setNative](value: Length) {
|
||||
org.nativescript.widgets.ViewHelper.setPaddingBottom(this.nativeTextViewProtected, Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderBottomWidth, 0));
|
||||
}
|
||||
[paddingBottomProperty.getDefault](): Length {
|
||||
return { value: this._defaultPaddingBottom, unit: "px" };
|
||||
}
|
||||
[paddingBottomProperty.setNative](value: Length) {
|
||||
org.nativescript.widgets.ViewHelper.setPaddingBottom(
|
||||
this.nativeTextViewProtected,
|
||||
Length.toDevicePixels(value, 0) +
|
||||
Length.toDevicePixels(this.style.borderBottomWidth, 0)
|
||||
);
|
||||
}
|
||||
|
||||
[paddingLeftProperty.getDefault](): Length {
|
||||
return { value: this._defaultPaddingLeft, unit: "px" };
|
||||
}
|
||||
[paddingLeftProperty.setNative](value: Length) {
|
||||
org.nativescript.widgets.ViewHelper.setPaddingLeft(this.nativeTextViewProtected, Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderLeftWidth, 0));
|
||||
}
|
||||
[paddingLeftProperty.getDefault](): Length {
|
||||
return { value: this._defaultPaddingLeft, unit: "px" };
|
||||
}
|
||||
[paddingLeftProperty.setNative](value: Length) {
|
||||
org.nativescript.widgets.ViewHelper.setPaddingLeft(
|
||||
this.nativeTextViewProtected,
|
||||
Length.toDevicePixels(value, 0) +
|
||||
Length.toDevicePixels(this.style.borderLeftWidth, 0)
|
||||
);
|
||||
}
|
||||
|
||||
_setNativeText(reset: boolean = false): void {
|
||||
if (reset) {
|
||||
this.nativeTextViewProtected.setText(null);
|
||||
_setNativeText(reset: boolean = false): void {
|
||||
if (reset) {
|
||||
this.nativeTextViewProtected.setText(null);
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let transformedText: any;
|
||||
if (this.formattedText) {
|
||||
transformedText = createSpannableStringBuilder(this.formattedText, this.style.fontSize);
|
||||
} else {
|
||||
const text = this.text;
|
||||
const stringValue = (text === null || text === undefined) ? "" : text.toString();
|
||||
transformedText = getTransformedText(stringValue, this.textTransform);
|
||||
}
|
||||
let transformedText: any;
|
||||
if (this.formattedText) {
|
||||
transformedText = createSpannableStringBuilder(
|
||||
this.formattedText,
|
||||
this.style.fontSize
|
||||
);
|
||||
} else {
|
||||
const text = this.text;
|
||||
const stringValue =
|
||||
text === null || text === undefined ? "" : text.toString();
|
||||
transformedText = getTransformedText(
|
||||
stringValue,
|
||||
this.textTransform
|
||||
);
|
||||
}
|
||||
|
||||
this.nativeTextViewProtected.setText(<any>transformedText);
|
||||
}
|
||||
this.nativeTextViewProtected.setText(<any>transformedText);
|
||||
}
|
||||
|
||||
_setTappableState(tappable: boolean) {
|
||||
if (this._tappable !== tappable) {
|
||||
this._tappable = tappable;
|
||||
if (this._tappable) {
|
||||
this.nativeViewProtected.setMovementMethod(android.text.method.LinkMovementMethod.getInstance());
|
||||
this.nativeViewProtected.setHighlightColor(null);
|
||||
}
|
||||
else {
|
||||
this.nativeViewProtected.setMovementMethod(this._defaultMovementMethod);
|
||||
}
|
||||
}
|
||||
}
|
||||
_setTappableState(tappable: boolean) {
|
||||
if (this._tappable !== tappable) {
|
||||
this._tappable = tappable;
|
||||
if (this._tappable) {
|
||||
this.nativeViewProtected.setMovementMethod(
|
||||
android.text.method.LinkMovementMethod.getInstance()
|
||||
);
|
||||
this.nativeViewProtected.setHighlightColor(null);
|
||||
} else {
|
||||
this.nativeViewProtected.setMovementMethod(
|
||||
this._defaultMovementMethod
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getCapitalizedString(str: string): string {
|
||||
let words = str.split(" ");
|
||||
let newWords = [];
|
||||
for (let i = 0, length = words.length; i < length; i++) {
|
||||
let word = words[i].toLowerCase();
|
||||
newWords.push(word.substr(0, 1).toUpperCase() + word.substring(1));
|
||||
}
|
||||
let words = str.split(" ");
|
||||
let newWords = [];
|
||||
for (let i = 0, length = words.length; i < length; i++) {
|
||||
let word = words[i].toLowerCase();
|
||||
newWords.push(word.substr(0, 1).toUpperCase() + word.substring(1));
|
||||
}
|
||||
|
||||
return newWords.join(" ");
|
||||
return newWords.join(" ");
|
||||
}
|
||||
|
||||
export function getTransformedText(text: string, textTransform: TextTransform): string {
|
||||
if (!text || !isString(text)) {
|
||||
return "";
|
||||
}
|
||||
export function getTransformedText(
|
||||
text: string,
|
||||
textTransform: TextTransform
|
||||
): string {
|
||||
if (!text || !isString(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
switch (textTransform) {
|
||||
case "uppercase":
|
||||
return text.toUpperCase();
|
||||
case "lowercase":
|
||||
return text.toLowerCase();
|
||||
case "capitalize":
|
||||
return getCapitalizedString(text);
|
||||
case "none":
|
||||
default:
|
||||
return text;
|
||||
}
|
||||
switch (textTransform) {
|
||||
case "uppercase":
|
||||
return text.toUpperCase();
|
||||
case "lowercase":
|
||||
return text.toLowerCase();
|
||||
case "capitalize":
|
||||
return getCapitalizedString(text);
|
||||
case "none":
|
||||
default:
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
function isStringTappable(formattedString: FormattedString) {
|
||||
if (!formattedString) {
|
||||
return false;
|
||||
}
|
||||
for (let i = 0, length = formattedString.spans.length; i < length; i++) {
|
||||
const span = formattedString.spans.getItem(i);
|
||||
if (span.tappable) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (!formattedString) {
|
||||
return false;
|
||||
}
|
||||
for (let i = 0, length = formattedString.spans.length; i < length; i++) {
|
||||
const span = formattedString.spans.getItem(i);
|
||||
if (span.tappable) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
function createSpannableStringBuilder(formattedString: FormattedString, defaultFontSize: number): android.text.SpannableStringBuilder {
|
||||
if (!formattedString || !formattedString.parent) {
|
||||
return null;
|
||||
}
|
||||
function createSpannableStringBuilder(
|
||||
formattedString: FormattedString,
|
||||
defaultFontSize: number
|
||||
): android.text.SpannableStringBuilder {
|
||||
if (!formattedString || !formattedString.parent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const ssb = new android.text.SpannableStringBuilder();
|
||||
for (let i = 0, spanStart = 0, spanLength = 0, length = formattedString.spans.length; i < length; i++) {
|
||||
const span = formattedString.spans.getItem(i);
|
||||
const text = span.text;
|
||||
const textTransform = (<TextBase>formattedString.parent).textTransform;
|
||||
let spanText = (text === null || text === undefined) ? "" : text.toString();
|
||||
if (textTransform && textTransform !== "none") {
|
||||
spanText = getTransformedText(spanText, textTransform);
|
||||
}
|
||||
const ssb = new android.text.SpannableStringBuilder();
|
||||
for (
|
||||
let i = 0,
|
||||
spanStart = 0,
|
||||
spanLength = 0,
|
||||
length = formattedString.spans.length;
|
||||
i < length;
|
||||
i++
|
||||
) {
|
||||
const span = formattedString.spans.getItem(i);
|
||||
const text = span.text;
|
||||
const textTransform = (<TextBase>formattedString.parent).textTransform;
|
||||
let spanText =
|
||||
text === null || text === undefined ? "" : text.toString();
|
||||
if (textTransform && textTransform !== "none") {
|
||||
spanText = getTransformedText(spanText, textTransform);
|
||||
}
|
||||
|
||||
spanLength = spanText.length;
|
||||
if (spanLength > 0) {
|
||||
ssb.insert(spanStart, spanText);
|
||||
setSpanModifiers(ssb, span, spanStart, spanStart + spanLength, defaultFontSize);
|
||||
spanStart += spanLength;
|
||||
}
|
||||
}
|
||||
spanLength = spanText.length;
|
||||
if (spanLength > 0) {
|
||||
ssb.insert(spanStart, spanText);
|
||||
setSpanModifiers(
|
||||
ssb,
|
||||
span,
|
||||
spanStart,
|
||||
spanStart + spanLength,
|
||||
defaultFontSize
|
||||
);
|
||||
spanStart += spanLength;
|
||||
}
|
||||
}
|
||||
|
||||
return ssb;
|
||||
return ssb;
|
||||
}
|
||||
|
||||
function setSpanModifiers(ssb: android.text.SpannableStringBuilder, span: Span, start: number, end: number, defaultFontSize: number): void {
|
||||
|
||||
Reference in New Issue
Block a user