开发过程中很多地方会用到文字自动缩放的功能,有些是设计如此,有些是防止策划文字配置超出预定的个数。其实都是一种情况,要在显示区域固定的情况下,显示出所有文字,当显示不全的时候就要缩小字号。
UGUI的Text提供了Best Fit选项支持自动缩放,开启后Text会自动在[Min Size, Max Size]区间中选择“合适”的字号进行渲染。本来挺好的一个功能,却被做成了奇葩(越发怀念NGUI了)。
在实际使用的时候,基本Best Fit只取到了Min或Max两个值。因为当一行放不下所有文字的时候,UGUI就会缩小字号。最终的结果就是,要么是一行的Max大字,要么是多行的Min小字,中间的过度几乎看不到。
在这方面NGUI UILabel的ShrinkContent做的就比较好,整个显示区域无法容纳全部内容的时候,才会缩小字号。本文的解决方案也是借鉴NGUI来的:
public class ShrinkText : Text
{
/// <summary>
/// 当前可见的文字行数
/// </summary>
public int VisibleLines { get; private set; }
private void _UseFitSettings()
{
TextGenerationSettings settings = GetGenerationSettings(rectTransform.rect.size);
settings.resizeTextForBestFit = false;
if (!resizeTextForBestFit)
{
cachedTextGenerator.Populate(text, settings);
return;
}
int minSize = resizeTextMinSize;
int txtLen = text.Length;
for (int i = resizeTextMaxSize; i >= minSize; --i)
{
settings.fontSize = i;
cachedTextGenerator.Populate(text, settings);
if (cachedTextGenerator.characterCountVisible == txtLen) break;
}
}
private readonly UIVertex[] _tmpVerts = new UIVertex[4];
protected override void OnPopulateMesh(VertexHelper toFill)
{
if (null == font) return;
m_DisableFontTextureRebuiltCallback = true;
_UseFitSettings();
Rect rect = rectTransform.rect;
Vector2 textAnchorPivot = GetTextAnchorPivot(alignment);
Vector2 zero = Vector2.zero;
zero.x = Mathf.Lerp(rect.xMin, rect.xMax, textAnchorPivot.x);
zero.y = Mathf.Lerp(rect.yMin, rect.yMax, textAnchorPivot.y);
Vector2 vector2 = PixelAdjustPoint(zero) - zero;
IList<UIVertex> verts = cachedTextGenerator.verts;
float num1 = 1f / pixelsPerUnit;
int num2 = verts.Count - 4;
toFill.Clear();
if (vector2 != Vector2.zero)
{
for (int index1 = 0; index1 < num2; ++index1)
{
int index2 = index1 & 3;
_tmpVerts[index2] = verts[index1];
_tmpVerts[index2].position *= num1;
_tmpVerts[index2].position.x += vector2.x;
_tmpVerts[index2].position.y += vector2.y;
if (index2 == 3)
toFill.AddUIVertexQuad(this._tmpVerts);
}
}
else
{
for (int index1 = 0; index1 < num2; ++index1)
{
int index2 = index1 & 3;
_tmpVerts[index2] = verts[index1];
_tmpVerts[index2].position *= num1;
if (index2 == 3)
toFill.AddUIVertexQuad(_tmpVerts);
}
}
m_DisableFontTextureRebuiltCallback = false;
VisibleLines = cachedTextGenerator.lineCount;
}
}
[2021-03-30 更新]
新项目使用Unity2020.3.1f1,发现这个脚本会少一个字符,大概看了下Text源码改了,把内容相应复制一下即可解决。变化的内容暂不深究了,没啥时间。
private void _UseFitSettings()
{
TextGenerationSettings settings = GetGenerationSettings(rectTransform.rect.size);
settings.resizeTextForBestFit = false;
if (!resizeTextForBestFit)
{
cachedTextGenerator.PopulateWithErrors(text, settings, gameObject);
return;
}
int minSize = resizeTextMinSize;
int txtLen = text.Length;
for (int i = resizeTextMaxSize; i >= minSize; --i)
{
settings.fontSize = i;
cachedTextGenerator.PopulateWithErrors(text, settings, gameObject);
if (cachedTextGenerator.characterCountVisible == txtLen) break;
}
}
protected override void OnPopulateMesh(VertexHelper toFill)
{
if (null == font) return;
m_DisableFontTextureRebuiltCallback = true;
_UseFitSettings();
// Apply the offset to the vertices
IList<UIVertex> verts = cachedTextGenerator.verts;
float unitsPerPixel = 1 / pixelsPerUnit;
int vertCount = verts.Count;
// We have no verts to process just return (case 1037923)
if (vertCount <= 0)
{
toFill.Clear();
return;
}
Vector2 roundingOffset = new Vector2(verts[0].position.x, verts[0].position.y) * unitsPerPixel;
roundingOffset = PixelAdjustPoint(roundingOffset) - roundingOffset;
toFill.Clear();
if (roundingOffset != Vector2.zero)
{
for (int i = 0; i < vertCount; ++i)
{
int tempVertsIndex = i & 3;
_tmpVerts[tempVertsIndex] = verts[i];
_tmpVerts[tempVertsIndex].position *= unitsPerPixel;
_tmpVerts[tempVertsIndex].position.x += roundingOffset.x;
_tmpVerts[tempVertsIndex].position.y += roundingOffset.y;
if (tempVertsIndex == 3)
toFill.AddUIVertexQuad(_tmpVerts);
}
}
else
{
for (int i = 0; i < vertCount; ++i)
{
int tempVertsIndex = i & 3;
_tmpVerts[tempVertsIndex] = verts[i];
_tmpVerts[tempVertsIndex].position *= unitsPerPixel;
if (tempVertsIndex == 3)
toFill.AddUIVertexQuad(_tmpVerts);
}
}
m_DisableFontTextureRebuiltCallback = false;
VisibleLines = cachedTextGenerator.lineCount;
}