@@ -361,7 +361,6 @@ export class Label extends LabelBase {
361
361
// }
362
362
363
363
// if (style.color && dict.size > 0) {
364
- // console.log('setTextDecorationAndTransform', style.color);
365
364
// // dict.set(NSForegroundColorAttributeName, style.color.ios);
366
365
// }
367
366
@@ -414,7 +413,11 @@ export class Label extends LabelBase {
414
413
const heightMode = layout . getMeasureSpecMode ( heightMeasureSpec ) ;
415
414
416
415
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
+ ) ;
418
421
}
419
422
420
423
const desiredSize = layout . measureNativeView ( nativeView , width , widthMode , height , heightMode ) ;
@@ -820,45 +823,59 @@ export class Label extends LabelBase {
820
823
}
821
824
}
822
825
823
- textViewDidChange ( textView : UITextView , width ?) {
824
-
826
+ textViewDidChange ( textView : UITextView , width ?, height ?) {
825
827
if ( this . autoFontSize ) {
826
828
if ( ( ! textView . attributedText && ! textView . text ) || CGSizeEqualToSize ( textView . bounds . size , CGSizeZero ) ) {
827
829
return ;
828
830
}
829
-
831
+ const nbLines = textView . textContainer . maximumNumberOfLines ;
830
832
// we need to reset verticalTextAlignment or computation will be wrong
831
833
this . updateTextContainerInset ( false ) ;
832
834
const textViewSize = textView . frame . size ;
833
835
const fixedWidth = width || textViewSize . width ;
836
+ const fixedHeight = height || textViewSize . height ;
834
837
835
838
const fontSize = this . style . fontSize || 17 ;
836
839
let expectFont : UIFont = ( this . style . fontInternal || Font . default ) . getUIFont ( UIFont . systemFontOfSize ( fontSize ) ) ;
837
840
//first reset the font size
838
841
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
+ ) {
842
856
const newFont = expectFont . fontWithSize ( expectFont . pointSize - 1 ) ;
843
857
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 ) {
846
860
expectFont = newFont ;
847
861
} else {
848
- textView . font = expectFont ;
862
+ textView . font = newFont ;
849
863
break ;
850
864
}
851
865
}
852
866
} 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
+ ) {
854
871
const newFont = expectFont . fontWithSize ( expectFont . pointSize + 1 ) ;
855
872
textView . font = newFont ;
856
- expectSize = textView . sizeThatFits ( CGSizeMake ( fixedWidth , Number . MAX_SAFE_INTEGER ) ) ;
873
+ size ( ) ;
857
874
858
- if ( expectSize . height <= textViewSize . height ) {
875
+ if ( expectSize . height <= textViewSize . height || expectSize . width <= textViewSize . width ) {
859
876
expectFont = newFont ;
860
877
} else {
861
- textView . font = expectFont ;
878
+ textView . font = newFont ;
862
879
break ;
863
880
}
864
881
}
0 commit comments