Skip to content

Instantly share code, notes, and snippets.

@empika
Last active March 9, 2018 08:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save empika/0b34d84fe2f7c4319bd9b6cc545dce3f to your computer and use it in GitHub Desktop.
Text that adds an ellipses to a truncated text field
using UnityEngine.UI;
public class TruncatableText : Text
{
public string ConcatText = "...";
private string truncatedText;
public override string text
{
get
{
return this.truncatedText;
}
set
{
if (string.IsNullOrEmpty(value))
{
if (string.IsNullOrEmpty(this.truncatedText))
return;
this.m_Text = "";
this.truncatedText = "";
this.SetVerticesDirty();
}
else
{
if (!(this.m_Text != value))
return;
this.m_Text = value;
this.SetVerticesDirty();
this.SetLayoutDirty();
}
}
}
protected override void OnPopulateMesh(VertexHelper toFill)
{
m_Text = m_Text.Trim();
this.cachedTextGenerator.PopulateWithErrors(m_Text, this.GetGenerationSettings(this.rectTransform.rect.size), this.gameObject);
int maxChars = this.cachedTextGenerator.characterCountVisible;
truncatedText = m_Text.Substring(0, maxChars);
if (maxChars < m_Text.Length)
{
truncatedText = truncatedText + ConcatText;
}
this.cachedTextGenerator.Invalidate();
this.cachedTextGenerator.PopulateWithErrors(truncatedText, this.GetGenerationSettings(this.rectTransform.rect.size), this.gameObject);
maxChars = this.cachedTextGenerator.characterCountVisible;
string secondTruncatedText = truncatedText.Substring(0, maxChars);
if (maxChars < truncatedText.Length)
{
truncatedText = secondTruncatedText + ConcatText;
}
this.cachedTextGenerator.Invalidate();
base.OnPopulateMesh(toFill);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment