UGUI实现文字两端对齐

2024-06-09 14:38
文章标签 文字 实现 对齐 ugui 两端

本文主要是介绍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);}elsevt.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实现文字两端对齐的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1045463

相关文章

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1