Skip to content

Commit b07d764

Browse files
committed
fix(ios): autoFontSize fix and make it faster
1 parent 880c75a commit b07d764

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/label.ios.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ export class Label extends LabelBase {
404404
if (this.autoFontSize) {
405405
this.textViewDidChange(
406406
nativeView,
407-
Math.floor(layout.toDeviceIndependentPixels(width)),
408-
Math.floor(layout.toDeviceIndependentPixels(height))
407+
layout.toDeviceIndependentPixels(width),
408+
layout.toDeviceIndependentPixels(height)
409409
);
410410
}
411411

@@ -823,12 +823,16 @@ export class Label extends LabelBase {
823823
) {
824824
return;
825825
}
826-
const nbLines = textView.textContainer.maximumNumberOfLines;
827-
// we need to reset verticalTextAlignment or computation will be wrong
828-
this.updateTextContainerInset(false);
826+
829827
const textViewSize = textView.frame.size;
830828
const fixedWidth = width || textViewSize.width;
831829
const fixedHeight = height || textViewSize.height;
830+
if (fixedWidth === 0 && fixedHeight === 0) {
831+
return;
832+
}
833+
const nbLines = textView.textContainer.maximumNumberOfLines;
834+
// we need to reset verticalTextAlignment or computation will be wrong
835+
this.updateTextContainerInset(false);
832836

833837
const fontSize = this.style.fontSize || 17;
834838
let expectFont: UIFont = (this.style.fontInternal || Font.default).getUIFont(UIFont.systemFontOfSize(fontSize));
@@ -843,15 +847,15 @@ export class Label extends LabelBase {
843847
}
844848
};
845849
size();
846-
if (expectSize.height > textViewSize.height || expectSize.width > textViewSize.width) {
850+
if (expectSize.height > fixedHeight || expectSize.width > fixedWidth) {
847851
while (
848-
(expectSize.height > textViewSize.height || expectSize.width > textViewSize.width) &&
852+
(expectSize.height > fixedHeight || expectSize.width > fixedWidth) &&
849853
expectFont.pointSize > (this.minFontSize || 12)
850854
) {
851855
const newFont = expectFont.fontWithSize(expectFont.pointSize - 1);
852856
textView.font = newFont;
853857
size();
854-
if (expectSize.height >= textViewSize.height || expectSize.width >= textViewSize.width) {
858+
if (expectSize.height >= fixedHeight || expectSize.width >= fixedWidth) {
855859
expectFont = newFont;
856860
} else {
857861
textView.font = newFont;
@@ -860,14 +864,14 @@ export class Label extends LabelBase {
860864
}
861865
} else {
862866
while (
863-
(expectSize.height < textViewSize.height || expectSize.width < textViewSize.width) &&
867+
(expectSize.height < fixedHeight || expectSize.width < fixedWidth) &&
864868
expectFont.pointSize < (this.maxFontSize || 200)
865869
) {
866870
const newFont = expectFont.fontWithSize(expectFont.pointSize + 1);
867871
textView.font = newFont;
868872
size();
869873

870-
if (expectSize.height <= textViewSize.height || expectSize.width <= textViewSize.width) {
874+
if (expectSize.height <= fixedHeight || expectSize.width <= fixedWidth) {
871875
expectFont = newFont;
872876
} else {
873877
textView.font = newFont;
@@ -878,16 +882,6 @@ export class Label extends LabelBase {
878882
this.updateTextContainerInset();
879883
}
880884
}
881-
_onSizeChanged(): void {
882-
const nativeView = this.nativeViewProtected;
883-
if (!nativeView) {
884-
return;
885-
}
886-
super._onSizeChanged();
887-
if (this.autoFontSize) {
888-
this.textViewDidChange(nativeView);
889-
}
890-
}
891885
[autoFontSizeProperty.setNative](value: boolean) {
892886
if (value && this.text) {
893887
this.textViewDidChange(this.nativeTextViewProtected);

0 commit comments

Comments
 (0)