UGUI实现文字两端对齐

Posted 天涯过客TYGK

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UGUI实现文字两端对齐相关的知识,希望对你有一定的参考价值。

参考博客
https://blog.csdn.net/feiyuezouni/article/details/85216983
在上述参考博客中,详尽讲解了Text文本显示字符串时候,文字间隔调整的原则,这里的代码也是在它的基础上进行修改出来的。

两端对齐

代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TextSpacingEasyTest : BaseMeshEffect 
	[SerializeField]
	Text text;
	[SerializeField]
	float textWidth;
	[SerializeField]
	bool AutoSpace=false;

	int fontSize =14;

	void Start()
	
		text = this.GetComponent<Text> ();
		var rect = text.GetComponent<RectTransform> ();
		textWidth = rect.rect.width;
		fontSize = text.fontSize;
	
	public float spacing = 0;
	public override void ModifyMesh(VertexHelper vh)
	
		List<UIVertex> vertexs = new List<UIVertex>();
		vh.GetUIVertexStream(vertexs);
		int vertexIndexCount = vertexs.Count;

		int worldspaceCount = vertexIndexCount/6-1; //所有文字间隔的数量

		if(AutoSpace) spacing = (textWidth-(worldspaceCount+1)*fontSize )/worldspaceCount;

		//vertexs 所有的顶点,i=6 也就是第一个顶点不参与计算
		for (int i = 6; i < vertexIndexCount; i++)
		
			UIVertex v = vertexs[i];

//			//设置单个文字的偏移量

			v.position += new Vector3(spacing * (i / 6), 0, 0);
			
			vertexs[i] = v;
			if (i % 6 <= 2)
			
				vh.SetUIVertex(v, (i / 6) * 4 + i % 6);
			
			if (i % 6 == 4)
			
				vh.SetUIVertex(v, (i / 6) * 4 + i % 6 - 1);
			
		
	



两端对齐结尾带符号

单行

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TextSpacingEasyTest : BaseMeshEffect 
	[SerializeField]
	Text text;
	[SerializeField]
	float textWidth;
	[SerializeField]
	bool AutoSpace=false;

	int fontSize =14;

	void Start()
	
		text = this.GetComponent<Text> ();
		var rect = text.GetComponent<RectTransform> ();
		textWidth = rect.rect.width;
		fontSize = text.fontSize;
	
	public float spacing = 0;
	public override void ModifyMesh(VertexHelper vh)
	
		List<UIVertex> vertexs = new List<UIVertex>();
		vh.GetUIVertexStream(vertexs);
		int vertexIndexCount = vertexs.Count;

		int worldspaceCount = vertexIndexCount/6-2; //所有文字间隔的数量,由于最后两个文字连在一起,所以间隔数比正常少一个

		if(AutoSpace) spacing = (textWidth-(worldspaceCount+2)*fontSize )/worldspaceCount;

		//vertexs 所有的顶点,i=6 也就是第一个顶点不参与计算
		for (int i = 6; i < vertexIndexCount; i++)
		
			UIVertex v = vertexs[i];

			//设置单个文字的偏移量,最后一个文字的偏移量和倒数第二个保持一致,实现最后两个字连在一起的效果
			if (i >= vertexIndexCount-6)
				v.position += new Vector3(spacing * worldspaceCount , 0, 0);
			else 
				v.position += new Vector3(spacing * (i / 6), 0, 0);
			
			vertexs[i] = v;
			if (i % 6 <= 2)
			
				vh.SetUIVertex(v, (i / 6) * 4 + i % 6);
			
			if (i % 6 == 4)
			
				vh.SetUIVertex(v, (i / 6) * 4 + i % 6 - 1);
			
		
	



多行

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Collections.Generic;

public class TextSpacingTest : BaseMeshEffect



	public class Line
	

		public int WorldCount get;private set;
		/// <summary>
		/// 起点索引
		/// </summary>
		public int StartVertexIndex get;private set;


		/// <summary>
		/// 终点索引
		/// </summary>
		public int EndVertexIndex get;private set;


		/// <summary>
		/// 该行占的点数目
		/// </summary>
		public int VertexCount get;private set;

		public Line(int worldCount,int startVertexIndex,int length)
		
			WorldCount = worldCount;
			StartVertexIndex = startVertexIndex;
			EndVertexIndex = length * 6 - 1 + startVertexIndex;
			VertexCount = length * 6;
		
	

	public float _textSpacing = 1f;

	[SerializeField]
	Text text;

	[SerializeField]
	float textWidth;

	[SerializeField]
	bool AutoSpace=false;

	[SerializeField]
	int SingleWorldWidth =14;

	void Start()
	
		text = this.GetComponent<Text> ();
		var rect = text.GetComponent<RectTransform> ();
		textWidth = rect.rect.width;

	



	public override void ModifyMesh(VertexHelper vh)
	
		if (!IsActive() || vh.currentVertCount == 0)
		
			return;
		

		Text text = GetComponent<Text>();
		if (text == null)
		
			Debug.LogError("Missing Text component");
			return;
		

		List<UIVertex> vertexs = new List<UIVertex>();
		vh.GetUIVertexStream(vertexs);
		int indexCount = vh.currentIndexCount;

		string[] lineTexts = text.text.Split('\\n');

		Line[] lines = new Line[lineTexts.Length];
		bool isSingleLine = lines.Length <= 1 ? true : false;
		SingleWorldWidth = text.fontSize;


		//根据lines数组中各个元素的长度计算每一行中第一个点的索引,每个字、字母、空母均占6个点
		if (isSingleLine) 
			lines[0] = new Line(lineTexts[0].Length,0, lineTexts[0].Length );
		 else 
			for (int i = 0; i < lines.Length; i++)
			
				//除最后一行外,vertexs对于前面几行都有回车符占了6个点
				if (i == 0)
				
					lines[i] = new Line(lineTexts[i].Length,0, lineTexts[i].Length + 1);
				
				else if(i > 0 && i < lines.Length - 1)
				
					lines[i] = new Line(lineTexts[i].Length,lines[i - 1].EndVertexIndex + 1, lineTexts[i].Length + 1);
				
				else
				
					lines[i] = new Line(lineTexts[i].Length,lines[i - 1].EndVertexIndex + 1, lineTexts[i].Length);
				
			
		


		UIVertex vt;
		int lineEndOffset = isSingleLine?6:12;
		int singleWorldWidth = text.fontSize;

		for (int i = 0; i < lines.Length; i++)
		
			//每一行文字直接需要动态变化的间隔数量,比如3个字中间有2个间隔,“属  性  :”
			//由于最后两个文字保持固定,所以三个字直接剩下1个间隔 ex: “属   性:”  
			///
			int worldSpaceCount = lines[i].WorldCount-2;    

			//计算间隔的大小,让文本框总长度,减去所有文字显示的宽度,得到空白的宽度,然后在去平均值,计算相邻两个文字之间的空隙
			if( AutoSpace ) 
					_textSpacing = ( textWidth - lines[i].WorldCount*SingleWorldWidth)/ worldSpaceCount;

			for (int j = lines[i].StartVertexIndex + 6; j <= lines[i].EndVertexIndex; j++)
			
				if (j < 0 || j >= vertexs.Count)
				
					continue;
				

				vt = vertexs[j];

				//当前行的第几个顶点
				int curLineVertexIndex = j - lines [i].StartVertexIndex;
				if (lines [i].WorldCount <= 2)
					continue;
				//设置偏移量,将一行最后两个字连在一起显示,如果不是单行的文字,注意解围的换行符
				if(curLineVertexIndex >= lineEndOffset && curLineVertexIndex >= lines[i].VertexCount-lineEndOffset)
					
					vt.position += new Vector3(_textSpacing * worldSpaceCount, 0, 0);

				else
					
					vt.position += new Vector3(_textSpacing * (curLineVertexIndex  / 6), 0, 0);


				vertexs[j] = vt;
				//以下注意点与索引的对应关系
				if (j % 6 <= 2)
				
					vh.SetUIVertex(vt, (j / 6) * 4 + j % 6);
				
				if (j % 6 == 4)
				
					vh.SetUIVertex(vt, (j / 6) * 4 + j % 6 - 1);
				
			
		
	


以上是关于UGUI实现文字两端对齐的主要内容,如果未能解决你的问题,请参考以下文章

Android 自定义TextView实现文字两端对齐

文字两端对齐

display:inline-block/text-align:justify下列表的两端对齐布局

小技巧CSS文字两端对齐

text-align: justify两端对齐布局

文字两端对齐