Skip to content

Commit 2c0e06d

Browse files
committed
fix(ios): fix for flexbox.
1 parent 27522e8 commit 2c0e06d

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

src/label.ios.ts

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class LabelUITextViewDelegateImpl extends NSObject implements UITextViewDelegate
139139
textViewDidChange?(textView: UITextView) {
140140
const owner = this._owner.get();
141141
if (owner) {
142-
owner.textViewDidChange(textView, undefined, undefined, true);
142+
owner.textViewDidChange(textView);
143143
}
144144
}
145145
}
@@ -326,19 +326,24 @@ export class Label extends LabelBase {
326326

327327
const height = layout.getMeasureSpecSize(heightMeasureSpec);
328328
const heightMode = layout.getMeasureSpecMode(heightMeasureSpec);
329+
let resetFont;
329330
if (this.autoFontSize) {
330331
const finiteWidth = widthMode === layout.EXACTLY;
331332
const finiteHeight = heightMode === layout.EXACTLY;
332333
if (!finiteWidth || !finiteHeight) {
333-
this.textViewDidChange(
334-
nativeView,
335-
layout.toDeviceIndependentPixels(width),
336-
layout.toDeviceIndependentPixels(height)
337-
);
334+
resetFont = this.updateAutoFontSize({
335+
textView: nativeView,
336+
width: layout.toDeviceIndependentPixels(width),
337+
height: layout.toDeviceIndependentPixels(height),
338+
onlyMeasure: true
339+
});
338340
}
339341
}
340342

341343
const desiredSize = layout.measureNativeView(nativeView, width, widthMode, height, heightMode);
344+
if (resetFont) {
345+
nativeView.font = resetFont;
346+
}
342347

343348
const labelWidth = widthMode === layout.AT_MOST ? Math.min(desiredSize.width, width) : desiredSize.width;
344349
// const labelHeight = heightMode === layout.AT_MOST ? Math.min(desiredSize.height, height) : desiredSize.height;
@@ -354,7 +359,7 @@ export class Label extends LabelBase {
354359
_onSizeChanged() {
355360
super._onSizeChanged();
356361
if (this.autoFontSize) {
357-
this.textViewDidChange(this.nativeTextViewProtected);
362+
this.updateAutoFontSize({ textView: this.nativeTextViewProtected });
358363
}
359364
}
360365
// _htmlTappable = false;
@@ -753,25 +758,39 @@ export class Label extends LabelBase {
753758

754759
fontSizeRatio = 1;
755760
_lastAutoSizeKey: string;
756-
textViewDidChange(textView: UITextView, width?, height?, force = false) {
761+
762+
updateAutoFontSize({
763+
textView,
764+
width,
765+
height,
766+
force = false,
767+
onlyMeasure = false
768+
}: {
769+
textView: UITextView;
770+
width?;
771+
height?;
772+
force?: boolean;
773+
onlyMeasure?: boolean;
774+
}) {
775+
let currentFont;
757776
if (textView && this.autoFontSize) {
758777
if (
759778
(!textView.attributedText && !textView.text) ||
760779
(width === undefined && height === undefined && CGSizeEqualToSize(textView.bounds.size, CGSizeZero))
761780
) {
762-
return;
781+
return currentFont;
763782
}
764-
765783
const textViewSize = textView.frame.size;
766784
const fixedWidth = Math.floor(width !== undefined ? width : textViewSize.width);
767785
const fixedHeight = Math.floor(height !== undefined ? height : textViewSize.height);
768786
if (fixedWidth === 0 || fixedHeight === 0) {
769-
return;
787+
return currentFont;
770788
}
771789
const autoSizeKey = fixedWidth + '_' + fixedHeight;
772790
if (!force && autoSizeKey === this._lastAutoSizeKey) {
773-
return;
791+
return null;
774792
}
793+
currentFont = textView.font;
775794
this._lastAutoSizeKey = autoSizeKey;
776795
const nbLines = textView.textContainer.maximumNumberOfLines;
777796
// we need to reset verticalTextAlignment or computation will be wrong
@@ -839,14 +858,21 @@ export class Label extends LabelBase {
839858
}
840859
}
841860
}
842-
this.fontSizeRatio = expectFont.pointSize / fontSize;
861+
if (!onlyMeasure) {
862+
this.fontSizeRatio = expectFont.pointSize / fontSize;
863+
}
864+
843865
this.updateTextContainerInset();
844866
}
867+
return currentFont;
868+
}
869+
textViewDidChange(textView: UITextView) {
870+
this.updateAutoFontSize({ textView, force: true });
845871
}
846872
[autoFontSizeProperty.setNative](value: boolean) {
847873
if (value) {
848874
if (this.isLayoutValid && (this.text || this.html || this.formattedText)) {
849-
this.textViewDidChange(this.nativeTextViewProtected, undefined, undefined, true);
875+
this.updateAutoFontSize({ textView: this.nativeTextViewProtected, force: true });
850876
}
851877
} else {
852878
this[fontInternalProperty.setNative](this.style.fontInternal);

0 commit comments

Comments
 (0)