Skip to content

Commit

Permalink
feat(ios): allow disabling text animations (#10505)
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni committed Apr 3, 2024
1 parent 7370912 commit 9ca4902
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
10 changes: 10 additions & 0 deletions packages/core/ui/text-base/index.d.ts
Expand Up @@ -99,6 +99,16 @@ export class TextBase extends View implements AddChildFromBuilder {
*/
paddingTop: CoreTypes.LengthType;

/**
* Specify wether the native text should be applied with or without animations
*/
iosTextAnimation: 'inherit' | boolean;

/**
* The value used when the iosTextAnimation is set to 'inherit'
*/
static iosTextAnimationFallback: boolean;

/**
* Called for every child element declared in xml.
* This method will add a child element (value) to current element.
Expand Down
64 changes: 37 additions & 27 deletions packages/core/ui/text-base/index.ios.ts
Expand Up @@ -291,39 +291,49 @@ export class TextBase extends TextBaseCommon {
this.nativeTextViewProtected.textColor = color;
}
}
_animationWrap(fn: () => void) {
const shouldAnimate = this.iosTextAnimation === 'inherit' ? TextBase.iosTextAnimationFallback : this.iosTextAnimation;
if (shouldAnimate) {
fn();
} else {
UIView.performWithoutAnimation(fn);
}
}

_setNativeText(reset = false): void {
if (reset) {
const nativeView = this.nativeTextViewProtected;
if (nativeView instanceof UIButton) {
// Clear attributedText or title won't be affected.
nativeView.setAttributedTitleForState(null, UIControlState.Normal);
nativeView.setTitleForState(null, UIControlState.Normal);
} else {
// Clear attributedText or text won't be affected.
nativeView.attributedText = null;
nativeView.text = null;
}
this._animationWrap(() => {
if (reset) {
const nativeView = this.nativeTextViewProtected;
if (nativeView instanceof UIButton) {
// Clear attributedText or title won't be affected.
nativeView.setAttributedTitleForState(null, UIControlState.Normal);
nativeView.setTitleForState(null, UIControlState.Normal);
} else {
// Clear attributedText or text won't be affected.
nativeView.attributedText = null;
nativeView.text = null;
}

return;
}
return;
}

const letterSpacing = this.style.letterSpacing ? this.style.letterSpacing : 0;
const lineHeight = this.style.lineHeight ? this.style.lineHeight : 0;
if (this.formattedText) {
this.nativeTextViewProtected.nativeScriptSetFormattedTextDecorationAndTransformLetterSpacingLineHeight(this.getFormattedStringDetails(this.formattedText) as any, letterSpacing, lineHeight);
} else {
// console.log('setTextDecorationAndTransform...')
const text = getTransformedText(isNullOrUndefined(this.text) ? '' : `${this.text}`, this.textTransform);
this.nativeTextViewProtected.nativeScriptSetTextDecorationAndTransformTextDecorationLetterSpacingLineHeight(text, this.style.textDecoration || '', letterSpacing, lineHeight);
const letterSpacing = this.style.letterSpacing ? this.style.letterSpacing : 0;
const lineHeight = this.style.lineHeight ? this.style.lineHeight : 0;
if (this.formattedText) {
this.nativeTextViewProtected.nativeScriptSetFormattedTextDecorationAndTransformLetterSpacingLineHeight(this.getFormattedStringDetails(this.formattedText) as any, letterSpacing, lineHeight);
} else {
// console.log('setTextDecorationAndTransform...')
const text = getTransformedText(isNullOrUndefined(this.text) ? '' : `${this.text}`, this.textTransform);
this.nativeTextViewProtected.nativeScriptSetTextDecorationAndTransformTextDecorationLetterSpacingLineHeight(text, this.style.textDecoration || '', letterSpacing, lineHeight);

if (!this.style?.color && majorVersion >= 13 && UIColor.labelColor) {
this._setColor(UIColor.labelColor);
if (!this.style?.color && majorVersion >= 13 && UIColor.labelColor) {
this._setColor(UIColor.labelColor);
}
}
}
if (this.style?.textStroke) {
this.nativeTextViewProtected.nativeScriptSetFormattedTextStrokeColor(Length.toDevicePixels(this.style.textStroke.width, 0), this.style.textStroke.color.ios);
}
if (this.style?.textStroke) {
this.nativeTextViewProtected.nativeScriptSetFormattedTextStrokeColor(Length.toDevicePixels(this.style.textStroke.width, 0), this.style.textStroke.color.ios);
}
});
}

createFormattedTextNative(value: FormattedString) {
Expand Down
18 changes: 17 additions & 1 deletion packages/core/ui/text-base/text-base-common.ts
@@ -1,6 +1,6 @@
// Types
import { PropertyChangeData } from '../../data/observable';
import { ViewBase } from '../core/view-base';
import { ViewBase, booleanConverter } from '../core/view-base';
import { FontStyleType, FontWeightType } from '../styling/font-interfaces';

// Requires.
Expand All @@ -24,6 +24,8 @@ export abstract class TextBaseCommon extends View implements TextBaseDefinition
public _isSingleLine: boolean;
public text: string;
public formattedText: FormattedString;
public iosTextAnimation: 'inherit' | boolean;
static iosTextAnimationFallback = true;

/***
* In the NativeScript Core; by default the nativeTextViewProtected points to the same value as nativeViewProtected.
Expand Down Expand Up @@ -236,6 +238,20 @@ export const formattedTextProperty = new Property<TextBaseCommon, FormattedStrin
});
formattedTextProperty.register(TextBaseCommon);

export const iosTextAnimationProperty = new Property<TextBaseCommon, 'inherit' | boolean>({
name: 'iosTextAnimation',
defaultValue: 'inherit',
affectsLayout: false,
valueConverter(value: string) {
try {
return booleanConverter(value);
} catch (e) {
return 'inherit';
}
},
});
iosTextAnimationProperty.register(TextBaseCommon);

function onFormattedTextPropertyChanged(textBase: TextBaseCommon, oldValue: FormattedString, newValue: FormattedString) {
if (oldValue) {
oldValue.off(Observable.propertyChangeEvent, textBase._onFormattedTextContentsChanged, textBase);
Expand Down

0 comments on commit 9ca4902

Please sign in to comment.