Skip to content

Commit 1712f4f

Browse files
committed
fix(ios): some autoFontSize optimisations
1 parent ce229c9 commit 1712f4f

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/label.ios.ts

+23-24
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class LabelUITextViewDelegateImpl extends NSObject implements UITextViewDelegate
138138
textViewDidChange?(textView: UITextView) {
139139
const owner = this._owner.get();
140140
if (owner) {
141-
owner.textViewDidChange(textView);
141+
owner.textViewDidChange(textView, undefined, undefined, true);
142142
}
143143
}
144144
}
@@ -412,10 +412,9 @@ export class Label extends LabelBase {
412412
const height = layout.getMeasureSpecSize(heightMeasureSpec);
413413
const heightMode = layout.getMeasureSpecMode(heightMeasureSpec);
414414
if (this.autoFontSize) {
415-
const finiteWidth: boolean = widthMode === layout.EXACTLY;
416-
const finiteHeight: boolean = heightMode === layout.EXACTLY;
415+
const finiteWidth = widthMode === layout.EXACTLY;
416+
const finiteHeight = heightMode === layout.EXACTLY;
417417
if (!finiteWidth || !finiteHeight) {
418-
this.needsAutoFontSize = true;
419418
this.textViewDidChange(
420419
nativeView,
421420
layout.toDeviceIndependentPixels(width),
@@ -437,6 +436,12 @@ export class Label extends LabelBase {
437436
this.setMeasuredDimension(widthAndState, heightAndState);
438437
}
439438
}
439+
_onSizeChanged() {
440+
super._onSizeChanged();
441+
if (this.autoFontSize) {
442+
this.textViewDidChange(this.nativeTextViewProtected);
443+
}
444+
}
440445
// _htmlTappable = false;
441446
// _htmlTapGestureRecognizer;
442447

@@ -560,26 +565,18 @@ export class Label extends LabelBase {
560565
}
561566
@needFormattedStringComputation
562567
[htmlProperty.setNative](value: string) {
563-
this.fontSizeRatio = 1;
564-
this.needsAutoFontSize = this.autoFontSize;
565568
this.updateHTMLString();
566569
}
567570
@needFormattedStringComputation
568571
[formattedTextProperty.setNative](value: string) {
569-
this.fontSizeRatio = 1;
570-
this.needsAutoFontSize = this.autoFontSize;
571572
super[formattedTextProperty.setNative](value);
572573
}
573574
@needFormattedStringComputation
574575
[letterSpacingProperty.setNative](value: number) {
575-
this.fontSizeRatio = 1;
576-
this.needsAutoFontSize = this.autoFontSize;
577576
super[letterSpacingProperty.setNative](value);
578577
}
579578
@needFormattedStringComputation
580579
[lineHeightProperty.setNative](value: number) {
581-
this.fontSizeRatio = 1;
582-
this.needsAutoFontSize = this.autoFontSize;
583580
super[lineHeightProperty.setNative](value);
584581
}
585582
// @needFormattedStringComputation
@@ -588,8 +585,6 @@ export class Label extends LabelBase {
588585
// }
589586
[fontInternalProperty.setNative](value: any) {
590587
const nativeView = this.nativeTextViewProtected;
591-
this.fontSizeRatio = 1;
592-
this.needsAutoFontSize = this.autoFontSize;
593588
const newFont: UIFont = value instanceof Font ? value.getUIFont(nativeView.font) : value;
594589
if (!this.formattedText && !this.html) {
595590
nativeView.font = newFont;
@@ -914,10 +909,9 @@ export class Label extends LabelBase {
914909
}
915910

916911
fontSizeRatio = 1;
917-
needsAutoFontSize = false;
918-
textViewDidChange(textView: UITextView, width?, height?) {
919-
if (this.autoFontSize && this.needsAutoFontSize) {
920-
this.needsAutoFontSize = false;
912+
_lastAutoSizeKey: string;
913+
textViewDidChange(textView: UITextView, width?, height?, force = false) {
914+
if (textView && this.autoFontSize) {
921915
if (
922916
(!textView.attributedText && !textView.text) ||
923917
(width === undefined && height === undefined && CGSizeEqualToSize(textView.bounds.size, CGSizeZero))
@@ -926,11 +920,16 @@ export class Label extends LabelBase {
926920
}
927921

928922
const textViewSize = textView.frame.size;
929-
const fixedWidth = width !== undefined ? width : textViewSize.width;
930-
const fixedHeight = height !== undefined ? height : textViewSize.height;
923+
const fixedWidth = Math.floor(width !== undefined ? width : textViewSize.width);
924+
const fixedHeight = Math.floor(height !== undefined ? height : textViewSize.height);
931925
if (fixedWidth === 0 || fixedHeight === 0) {
932926
return;
933927
}
928+
const autoSizeKey = fixedWidth + '_' + fixedHeight;
929+
if (!force && autoSizeKey === this._lastAutoSizeKey) {
930+
return;
931+
}
932+
this._lastAutoSizeKey = autoSizeKey;
934933
const nbLines = textView.textContainer.maximumNumberOfLines;
935934
// we need to reset verticalTextAlignment or computation will be wrong
936935
this.updateTextContainerInset(false);
@@ -1002,10 +1001,10 @@ export class Label extends LabelBase {
10021001
}
10031002
}
10041003
[autoFontSizeProperty.setNative](value: boolean) {
1005-
if (value && (this.text || this.html || this.formattedText)) {
1006-
this.fontSizeRatio = 1;
1007-
this.needsAutoFontSize = true;
1008-
this.textViewDidChange(this.nativeTextViewProtected);
1004+
if (value) {
1005+
if (this.isLayoutValid && (this.text || this.html || this.formattedText)) {
1006+
this.textViewDidChange(this.nativeTextViewProtected, undefined, undefined, true);
1007+
}
10091008
} else {
10101009
this[fontInternalProperty.setNative](this.style.fontInternal);
10111010
}

0 commit comments

Comments
 (0)