mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-21 16:00:24 +00:00
增加TextLimit组件可以设置AxisLabel的文本自适应
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
@@ -27,7 +28,7 @@ namespace XCharts
|
||||
[SerializeField] private int m_FontSize;
|
||||
[SerializeField] private FontStyle m_FontStyle;
|
||||
[SerializeField] private bool m_ForceENotation = false;
|
||||
|
||||
[SerializeField] private TextLimit m_TextLimit = new TextLimit();
|
||||
|
||||
/// <summary>
|
||||
/// Set this to false to prevent the axis label from appearing.
|
||||
@@ -78,6 +79,10 @@ namespace XCharts
|
||||
/// 是否强制使用科学计数法格式化显示数值。默认为false,当小数精度大于3时才采用科学计数法。
|
||||
/// </summary>
|
||||
public bool forceENotation { get { return m_ForceENotation; } set { m_ForceENotation = value; } }
|
||||
/// <summary>
|
||||
/// 文本限制。
|
||||
/// </summary>
|
||||
public TextLimit textLimit { get { return m_TextLimit; } }
|
||||
|
||||
public static AxisLabel defaultAxisLabel
|
||||
{
|
||||
@@ -107,6 +112,7 @@ namespace XCharts
|
||||
m_FontSize = other.fontSize;
|
||||
m_FontStyle = other.fontStyle;
|
||||
m_Formatter = other.formatter;
|
||||
m_TextLimit.Copy(other.textLimit);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
@@ -125,7 +131,8 @@ namespace XCharts
|
||||
m_FontSize == other.fontSize &&
|
||||
m_FontStyle == other.fontStyle &&
|
||||
m_ForceENotation == other.forceENotation &&
|
||||
m_Formatter == other.formatter;
|
||||
m_Formatter.Equals(other.formatter) &&
|
||||
m_TextLimit.Equals(other.textLimit);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
@@ -133,16 +140,24 @@ namespace XCharts
|
||||
return base.GetHashCode();
|
||||
}
|
||||
|
||||
public void SetRelatedText(Text txt, float labelWidth)
|
||||
{
|
||||
m_TextLimit.SetRelatedText(txt,labelWidth);
|
||||
}
|
||||
|
||||
public string GetFormatterContent(string category)
|
||||
{
|
||||
if (string.IsNullOrEmpty(category)) return category;
|
||||
if (string.IsNullOrEmpty(m_Formatter))
|
||||
return category;
|
||||
{
|
||||
return m_TextLimit.GetLimitContent(category);
|
||||
}
|
||||
else
|
||||
{
|
||||
var content = m_Formatter.Replace("{value}", category);
|
||||
content = content.Replace("\\n", "\n");
|
||||
content = content.Replace("<br/>", "\n");
|
||||
return content;
|
||||
return m_TextLimit.GetLimitContent(content);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
136
Runtime/Component/Sub/TextLimit.cs
Normal file
136
Runtime/Component/Sub/TextLimit.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
/******************************************/
|
||||
/* */
|
||||
/* Copyright (c) 2018 monitor1394 */
|
||||
/* https://github.com/monitor1394 */
|
||||
/* */
|
||||
/******************************************/
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
/// <summary>
|
||||
/// 文本字符限制和自适应。当文本长度超过设定的长度时进行裁剪,并将后缀附加在最后。
|
||||
/// 只在类目轴中有效。
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class TextLimit : SubComponent
|
||||
{
|
||||
[SerializeField] private bool m_Enable = true;
|
||||
[SerializeField] private float m_MaxWidth = 0;
|
||||
[SerializeField] private float m_Gap = 10;
|
||||
[SerializeField] private string m_Suffix = "...";
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用文本自适应。默认为true。
|
||||
/// </summary>
|
||||
public bool enable { get { return m_Enable; } set { m_Enable = value; } }
|
||||
/// <summary>
|
||||
/// 设定最大宽度。默认为0表示自动获取,否则表示自定义。当文本的宽度大于该值进行裁剪。
|
||||
/// </summary>
|
||||
public float maxWidth { get { return m_MaxWidth; } set { m_MaxWidth = value; } }
|
||||
/// <summary>
|
||||
/// 两边留白像素距离。默认为10
|
||||
/// </summary>
|
||||
public float gap { get { return m_Gap; } set { m_Gap = value; } }
|
||||
/// <summary>
|
||||
/// 长度超出时的后缀。
|
||||
/// </summary>
|
||||
public string suffix { get { return m_Suffix; } set { m_Suffix = value; } }
|
||||
|
||||
private Text m_RelatedText;
|
||||
private TextGenerationSettings m_RelatedTextSettings;
|
||||
private float m_RelatedTextWidth = 0;
|
||||
|
||||
public void Copy(TextLimit other)
|
||||
{
|
||||
m_Enable = other.enable;
|
||||
m_Gap = other.gap;
|
||||
m_Suffix = other.suffix;
|
||||
m_MaxWidth = other.maxWidth;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null || GetType() != obj.GetType())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var other = (TextLimit)obj;
|
||||
return m_Enable == other.enable &&
|
||||
m_MaxWidth == other.maxWidth &&
|
||||
m_Gap == other.gap &&
|
||||
m_Suffix.Equals(other.suffix);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return base.GetHashCode();
|
||||
}
|
||||
|
||||
public void SetRelatedText(Text txt, float labelWidth)
|
||||
{
|
||||
m_RelatedText = txt;
|
||||
m_RelatedTextSettings = txt.GetGenerationSettings(Vector2.zero);
|
||||
m_RelatedTextWidth = labelWidth;
|
||||
}
|
||||
|
||||
|
||||
public string GetLimitContent(string content)
|
||||
{
|
||||
float checkWidth = m_MaxWidth > 0 ? m_MaxWidth : m_RelatedTextWidth;
|
||||
if (m_RelatedText == null || checkWidth <= 0) return content;
|
||||
else
|
||||
{
|
||||
if (m_Enable)
|
||||
{
|
||||
float len = m_RelatedText.cachedTextGenerator.GetPreferredWidth(content, m_RelatedTextSettings);
|
||||
float suffixLen = m_RelatedText.cachedTextGenerator.GetPreferredWidth(suffix, m_RelatedTextSettings);
|
||||
if (len > checkWidth - m_Gap * 2 - suffixLen)
|
||||
{
|
||||
return content.Substring(0, GetAdaptLength(content, suffixLen)) + suffix;
|
||||
}
|
||||
else
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int GetAdaptLength(string content, float suffixLen)
|
||||
{
|
||||
int start = 0;
|
||||
int middle = content.Length / 2;
|
||||
int end = content.Length;
|
||||
float checkWidth = m_MaxWidth > 0 ? m_MaxWidth : m_RelatedTextWidth;
|
||||
float limit = checkWidth - m_Gap * 2 - suffixLen;
|
||||
if (limit < 0) return 0;
|
||||
float len = 0;
|
||||
while (len != limit && middle != start)
|
||||
{
|
||||
len = m_RelatedText.cachedTextGenerator.GetPreferredWidth(content.Substring(0, middle), m_RelatedTextSettings);
|
||||
if (len < limit)
|
||||
{
|
||||
start = middle;
|
||||
}
|
||||
else if (len > limit)
|
||||
{
|
||||
end = middle;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
middle = (start + end) / 2;
|
||||
}
|
||||
return middle;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Component/Sub/TextLimit.cs.meta
Normal file
11
Runtime/Component/Sub/TextLimit.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3f49509a5de044535b1dd3f192f7008c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user