using UnityEngine;
using UnityEngine.UI;
namespace UVC.Extension
{
///
/// Unity UI Text ÄÄÆ÷³ÍÆ®¿¡ ´ëÇÑ È®Àå ¸Þ¼µå ¸ðÀ½
///
public static class TextEx
{
///
/// Text ÄÄÆ÷³ÍÆ®¿¡ ÅØ½ºÆ®¸¦ ¼³Á¤ÇϵÇ, ÁÖ¾îÁø ¿µ¿ªÀ» ÃʰúÇÒ °æ¿ì ¸»ÁÙÀÓÇ¥(...)¸¦ Ãß°¡ÇÕ´Ï´Ù.
///
/// ´ë»ó Text ÄÄÆ÷³ÍÆ®
/// Ç¥½ÃÇÒ ÅØ½ºÆ® ³»¿ë
///
///
/// // »ç¿ë ¿¹½Ã
/// Text myText = GetComponent();
/// myText.SetTextWithEllipsis("ÀÌ ÅØ½ºÆ®°¡ ³Ê¹« ±æ¸é ÀÚµ¿À¸·Î ÁÙ¿©Áý´Ï´Ù...");
///
///
public static void SetTextWithEllipsis(this Text txt, string value)
{
// TextGenerator »ý¼º ¹× ÅØ½ºÆ® ÄÄÆ÷³ÍÆ®ÀÇ ¼³Á¤°ª °¡Á®¿À±â
TextGenerator generator = new TextGenerator();
RectTransform rectTrans = txt.rectTransform;
TextGenerationSettings settings = txt.GetGenerationSettings(rectTrans.rect.size);
generator.Populate(value, settings);
// Ç¥½Ã °¡´ÉÇÑ ¹®ÀÚ ¼ö °è»ê
int characterCountVisible = generator.characterCountVisible;
string updatedText = value;
// ÅØ½ºÆ®°¡ Ç¥½Ã ¿µ¿ªÀ» ÃʰúÇÏ´Â °æ¿ì ¸»ÁÙÀÓÇ¥ ó¸®
if (value.Length > characterCountVisible && characterCountVisible >= 3)
{
updatedText = value.Substring(0, characterCountVisible - 3);
updatedText += "...";
}
// °á°ú ÅØ½ºÆ® ¼³Á¤
txt.text = updatedText;
}
///
/// ÅØ½ºÆ® ÄÄÆ÷³ÍÆ®ÀÇ ³»¿ëÀ» ÁöÁ¤µÈ ÃÖ´ë ±æÀÌ·Î Á¦ÇÑÇÕ´Ï´Ù.
///
/// ´ë»ó Text ÄÄÆ÷³ÍÆ®
/// Ç¥½ÃÇÒ ÅØ½ºÆ®
/// ÃÖ´ë ±ÛÀÚ¼ö
///
///
/// Text myText = GetComponent();
/// myText.SetTextWithMaxLength("±ä ÅØ½ºÆ® ³»¿ëÀÔ´Ï´Ù", 10); // "±ä ÅØ½ºÆ® ..."
///
///
public static void SetTextWithMaxLength(this Text txt, string value, int maxLength)
{
if (string.IsNullOrEmpty(value) || value.Length <= maxLength)
{
txt.text = value;
return;
}
txt.text = value.Substring(0, maxLength - 3) + "...";
}
///
/// ÅØ½ºÆ® ÄÄÆ÷³ÍÆ®¿¡ ¼ýÀÚ¸¦ õ ´ÜÀ§ ±¸ºÐÀÚ(,)¿Í ÇÔ²² Ç¥½ÃÇÕ´Ï´Ù.
///
/// ´ë»ó Text ÄÄÆ÷³ÍÆ®
/// Ç¥½ÃÇÒ ¼ýÀÚ
///
///
/// Text scoreText = GetComponent();
/// scoreText.SetNumberWithCommas(1000000); // "1,000,000" Ç¥½Ã
///
///
public static void SetNumberWithCommas(this Text txt, int number)
{
txt.text = string.Format("{0:#,0}", number);
}
///
/// ÅØ½ºÆ®°¡ Ç¥½Ã ¿µ¿ª¿¡ ¸Â°Ô ÀÚµ¿À¸·Î ÆùÆ® Å©±â¸¦ Á¶ÀýÇÕ´Ï´Ù.
///
/// ´ë»ó Text ÄÄÆ÷³ÍÆ®
/// Ç¥½ÃÇÒ ÅØ½ºÆ®
/// ÃÖ¼Ò ÆùÆ® Å©±â
/// ÃÖ´ë ÆùÆ® Å©±â
///
///
/// Text titleText = GetComponent();
/// titleText.SetTextWithBestFit("Á¦¸ñ ÅØ½ºÆ®", 10, 30);
///
///
public static void SetTextWithBestFit(this Text txt, string value, int minSize = 10, int maxSize = 40)
{
txt.text = value;
txt.resizeTextForBestFit = true;
txt.resizeTextMinSize = minSize;
txt.resizeTextMaxSize = maxSize;
}
}
}