/TruncatableText.cs Secret
Last active
March 9, 2018 08:39
Star
You must be signed in to star a gist
Text that adds an ellipses to a truncated text field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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