Skip to content

Commit

Permalink
Fix adjustsFontSizeToFit for strings with a single character
Browse files Browse the repository at this point in the history
  • Loading branch information
j-piasecki committed Nov 7, 2024
1 parent fb69f40 commit ec2182b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ - (RCTTextSizeComparisonOptions)compareToSize:(CGSize)size thresholdRatio:(CGFlo
NSLayoutManager *layoutManager = self.layoutManagers.firstObject;
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;

// A workaround for truncatedGlyphRangeInLineFragmentForGlyphAtIndex returning NSNotFound when text has only
// one character and it gets truncated
if ([self length] == 1) {
CGSize characterSize = [[self string] sizeWithAttributes:[self attributesAtIndex:0 effectiveRange:nil]];
if (characterSize.width > size.width) {
return RCTTextSizeComparisonLarger;
}
}

[layoutManager ensureLayoutForTextContainer:textContainer];

// Does it fit the text container?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,8 @@ private static Layout createLayout(
&& maximumNumberOfLines != 0
&& layout.getLineCount() > maximumNumberOfLines)
|| (heightYogaMeasureMode != YogaMeasureMode.UNDEFINED
&& layout.getHeight() > height))) {
&& layout.getHeight() > height)
|| (text.length() == 1 && layout.getPaint().measureText(text.toString()) > width))) {
// TODO: We could probably use a smarter algorithm here. This will require 0(n)
// measurements based on the number of points the font size needs to be reduced by.
currentFontSize -= Math.max(1, (int) PixelUtil.toPixelFromDIP(1));
Expand All @@ -648,6 +649,7 @@ private static Layout createLayout(
text.getSpanFlags(span));
text.removeSpan(span);
}
boring = BoringLayout.isBoring(text, paint);
layout =
createLayout(
text,
Expand Down

0 comments on commit ec2182b

Please sign in to comment.