Skip to content

Commit 9ca4902

Browse files
authored
feat(ios): allow disabling text animations (#10505)
1 parent 7370912 commit 9ca4902

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

packages/core/ui/text-base/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ export class TextBase extends View implements AddChildFromBuilder {
9999
*/
100100
paddingTop: CoreTypes.LengthType;
101101

102+
/**
103+
* Specify wether the native text should be applied with or without animations
104+
*/
105+
iosTextAnimation: 'inherit' | boolean;
106+
107+
/**
108+
* The value used when the iosTextAnimation is set to 'inherit'
109+
*/
110+
static iosTextAnimationFallback: boolean;
111+
102112
/**
103113
* Called for every child element declared in xml.
104114
* This method will add a child element (value) to current element.

packages/core/ui/text-base/index.ios.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -291,39 +291,49 @@ export class TextBase extends TextBaseCommon {
291291
this.nativeTextViewProtected.textColor = color;
292292
}
293293
}
294+
_animationWrap(fn: () => void) {
295+
const shouldAnimate = this.iosTextAnimation === 'inherit' ? TextBase.iosTextAnimationFallback : this.iosTextAnimation;
296+
if (shouldAnimate) {
297+
fn();
298+
} else {
299+
UIView.performWithoutAnimation(fn);
300+
}
301+
}
294302

295303
_setNativeText(reset = false): void {
296-
if (reset) {
297-
const nativeView = this.nativeTextViewProtected;
298-
if (nativeView instanceof UIButton) {
299-
// Clear attributedText or title won't be affected.
300-
nativeView.setAttributedTitleForState(null, UIControlState.Normal);
301-
nativeView.setTitleForState(null, UIControlState.Normal);
302-
} else {
303-
// Clear attributedText or text won't be affected.
304-
nativeView.attributedText = null;
305-
nativeView.text = null;
306-
}
304+
this._animationWrap(() => {
305+
if (reset) {
306+
const nativeView = this.nativeTextViewProtected;
307+
if (nativeView instanceof UIButton) {
308+
// Clear attributedText or title won't be affected.
309+
nativeView.setAttributedTitleForState(null, UIControlState.Normal);
310+
nativeView.setTitleForState(null, UIControlState.Normal);
311+
} else {
312+
// Clear attributedText or text won't be affected.
313+
nativeView.attributedText = null;
314+
nativeView.text = null;
315+
}
307316

308-
return;
309-
}
317+
return;
318+
}
310319

311-
const letterSpacing = this.style.letterSpacing ? this.style.letterSpacing : 0;
312-
const lineHeight = this.style.lineHeight ? this.style.lineHeight : 0;
313-
if (this.formattedText) {
314-
this.nativeTextViewProtected.nativeScriptSetFormattedTextDecorationAndTransformLetterSpacingLineHeight(this.getFormattedStringDetails(this.formattedText) as any, letterSpacing, lineHeight);
315-
} else {
316-
// console.log('setTextDecorationAndTransform...')
317-
const text = getTransformedText(isNullOrUndefined(this.text) ? '' : `${this.text}`, this.textTransform);
318-
this.nativeTextViewProtected.nativeScriptSetTextDecorationAndTransformTextDecorationLetterSpacingLineHeight(text, this.style.textDecoration || '', letterSpacing, lineHeight);
320+
const letterSpacing = this.style.letterSpacing ? this.style.letterSpacing : 0;
321+
const lineHeight = this.style.lineHeight ? this.style.lineHeight : 0;
322+
if (this.formattedText) {
323+
this.nativeTextViewProtected.nativeScriptSetFormattedTextDecorationAndTransformLetterSpacingLineHeight(this.getFormattedStringDetails(this.formattedText) as any, letterSpacing, lineHeight);
324+
} else {
325+
// console.log('setTextDecorationAndTransform...')
326+
const text = getTransformedText(isNullOrUndefined(this.text) ? '' : `${this.text}`, this.textTransform);
327+
this.nativeTextViewProtected.nativeScriptSetTextDecorationAndTransformTextDecorationLetterSpacingLineHeight(text, this.style.textDecoration || '', letterSpacing, lineHeight);
319328

320-
if (!this.style?.color && majorVersion >= 13 && UIColor.labelColor) {
321-
this._setColor(UIColor.labelColor);
329+
if (!this.style?.color && majorVersion >= 13 && UIColor.labelColor) {
330+
this._setColor(UIColor.labelColor);
331+
}
322332
}
323-
}
324-
if (this.style?.textStroke) {
325-
this.nativeTextViewProtected.nativeScriptSetFormattedTextStrokeColor(Length.toDevicePixels(this.style.textStroke.width, 0), this.style.textStroke.color.ios);
326-
}
333+
if (this.style?.textStroke) {
334+
this.nativeTextViewProtected.nativeScriptSetFormattedTextStrokeColor(Length.toDevicePixels(this.style.textStroke.width, 0), this.style.textStroke.color.ios);
335+
}
336+
});
327337
}
328338

329339
createFormattedTextNative(value: FormattedString) {

packages/core/ui/text-base/text-base-common.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Types
22
import { PropertyChangeData } from '../../data/observable';
3-
import { ViewBase } from '../core/view-base';
3+
import { ViewBase, booleanConverter } from '../core/view-base';
44
import { FontStyleType, FontWeightType } from '../styling/font-interfaces';
55

66
// Requires.
@@ -24,6 +24,8 @@ export abstract class TextBaseCommon extends View implements TextBaseDefinition
2424
public _isSingleLine: boolean;
2525
public text: string;
2626
public formattedText: FormattedString;
27+
public iosTextAnimation: 'inherit' | boolean;
28+
static iosTextAnimationFallback = true;
2729

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

241+
export const iosTextAnimationProperty = new Property<TextBaseCommon, 'inherit' | boolean>({
242+
name: 'iosTextAnimation',
243+
defaultValue: 'inherit',
244+
affectsLayout: false,
245+
valueConverter(value: string) {
246+
try {
247+
return booleanConverter(value);
248+
} catch (e) {
249+
return 'inherit';
250+
}
251+
},
252+
});
253+
iosTextAnimationProperty.register(TextBaseCommon);
254+
239255
function onFormattedTextPropertyChanged(textBase: TextBaseCommon, oldValue: FormattedString, newValue: FormattedString) {
240256
if (oldValue) {
241257
oldValue.off(Observable.propertyChangeEvent, textBase._onFormattedTextContentsChanged, textBase);

0 commit comments

Comments
 (0)