Skip to content

Commit 08ed0cb

Browse files
committed
fix(ios): support autosize for 1 line labels (based on width)
1 parent 4c335fe commit 08ed0cb

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

Diff for: src/label.ios.ts

+32-15
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ export class Label extends LabelBase {
361361
// }
362362

363363
// if (style.color && dict.size > 0) {
364-
// console.log('setTextDecorationAndTransform', style.color);
365364
// // dict.set(NSForegroundColorAttributeName, style.color.ios);
366365
// }
367366

@@ -414,7 +413,11 @@ export class Label extends LabelBase {
414413
const heightMode = layout.getMeasureSpecMode(heightMeasureSpec);
415414

416415
if (this.autoFontSize) {
417-
this.textViewDidChange(nativeView, Math.floor(layout.toDeviceIndependentPixels(width)));
416+
this.textViewDidChange(
417+
nativeView,
418+
Math.floor(layout.toDeviceIndependentPixels(width)),
419+
Math.floor(layout.toDeviceIndependentPixels(height))
420+
);
418421
}
419422

420423
const desiredSize = layout.measureNativeView(nativeView, width, widthMode, height, heightMode);
@@ -820,45 +823,59 @@ export class Label extends LabelBase {
820823
}
821824
}
822825

823-
textViewDidChange(textView: UITextView, width?) {
824-
826+
textViewDidChange(textView: UITextView, width?, height?) {
825827
if (this.autoFontSize) {
826828
if ((!textView.attributedText && !textView.text) || CGSizeEqualToSize(textView.bounds.size, CGSizeZero)) {
827829
return;
828830
}
829-
831+
const nbLines = textView.textContainer.maximumNumberOfLines;
830832
// we need to reset verticalTextAlignment or computation will be wrong
831833
this.updateTextContainerInset(false);
832834
const textViewSize = textView.frame.size;
833835
const fixedWidth = width || textViewSize.width;
836+
const fixedHeight = height || textViewSize.height;
834837

835838
const fontSize = this.style.fontSize || 17;
836839
let expectFont: UIFont = (this.style.fontInternal || Font.default).getUIFont(UIFont.systemFontOfSize(fontSize));
837840
//first reset the font size
838841
textView.font = expectFont;
839-
let expectSize = textView.sizeThatFits(CGSizeMake(fixedWidth, Number.MAX_SAFE_INTEGER));
840-
if (expectSize.height > textViewSize.height) {
841-
while (expectSize.height > textViewSize.height && expectFont.pointSize > (this.minFontSize || 12)) {
842+
let expectSize;
843+
const size = () => {
844+
if (nbLines === 1) {
845+
expectSize = textView.sizeThatFits(CGSizeMake(Number.MAX_SAFE_INTEGER, fixedHeight));
846+
} else {
847+
expectSize = textView.sizeThatFits(CGSizeMake(fixedWidth, Number.MAX_SAFE_INTEGER));
848+
}
849+
};
850+
size();
851+
if (expectSize.height > textViewSize.height || expectSize.width > textViewSize.width) {
852+
while (
853+
(expectSize.height > textViewSize.height || expectSize.width > textViewSize.width) &&
854+
expectFont.pointSize > (this.minFontSize || 12)
855+
) {
842856
const newFont = expectFont.fontWithSize(expectFont.pointSize - 1);
843857
textView.font = newFont;
844-
expectSize = textView.sizeThatFits(CGSizeMake(fixedWidth, Number.MAX_SAFE_INTEGER));
845-
if (expectSize.height >= textViewSize.height) {
858+
size();
859+
if (expectSize.height >= textViewSize.height || expectSize.width >= textViewSize.width) {
846860
expectFont = newFont;
847861
} else {
848-
textView.font = expectFont;
862+
textView.font = newFont;
849863
break;
850864
}
851865
}
852866
} else {
853-
while (expectSize.height < textViewSize.height && expectFont.pointSize < (this.maxFontSize || 200)) {
867+
while (
868+
(expectSize.height < textViewSize.height || expectSize.width < textViewSize.width) &&
869+
expectFont.pointSize < (this.maxFontSize || 200)
870+
) {
854871
const newFont = expectFont.fontWithSize(expectFont.pointSize + 1);
855872
textView.font = newFont;
856-
expectSize = textView.sizeThatFits(CGSizeMake(fixedWidth, Number.MAX_SAFE_INTEGER));
873+
size();
857874

858-
if (expectSize.height <= textViewSize.height) {
875+
if (expectSize.height <= textViewSize.height || expectSize.width <= textViewSize.width) {
859876
expectFont = newFont;
860877
} else {
861-
textView.font = expectFont;
878+
textView.font = newFont;
862879
break;
863880
}
864881
}

0 commit comments

Comments
 (0)