当前位置:网站首页>指定宽度截取字符串
指定宽度截取字符串
2022-07-29 19:50:00 【艺术就是CtrlC】
开发过程中遇到一个需求,就是在聊天栏中,对于超出聊天框的文字需要省略并添加省略号,比如输入"dasda这是一个例子にほんçai222!]]//*",需要截取并显示为"dasda这是一个例子にほんç…",注意:字符串长度不等于字符串宽度,以下提供两种情况的代码,第一种是纯C#调用,第二种是Lua和C#
①C#
//获取字符串宽度
public float GetWidth(Text textComponent, string textContent)
{
Text text = textComponent;
Font font = text.font;
if (font == null)
{
return 0;
}
int fontSize = font.fontSize;
FontStyle fontStyle = text.fontStyle;
font.RequestCharactersInTexture(textContent, fontSize, fontStyle);
CharacterInfo characterInfo;
float width = 0f;
for (int i = 0; i < textContent.Length; i++)
{
font.GetCharacterInfo(textContent[i], out characterInfo, fontSize);
width += characterInfo.advance;
}
return width;
}
//拆分字符串
public Array SplitString(string s)
{
char[] strCharArr = s.ToCharArray();
return strCharArr;
}
//获取允许显示的文本内容 textContent = '这是一个参数示例', textComponent = self.Text.chatTxt1, widthOriginal = 393, textTarget = '...'
//textContent:原文本内容
//textComponent:text组件
//widthOriginal:text组件节点的width(通过直接传入这个值, 来避免在此方法内频繁调用GetComponent)
//widthTarget:想要显示的区域宽度(比如省略号宽度10, 那width就是10)和textTarget二选一传入
//textTarget:想要添加的字符串(比如添加省略号'...')和widthTarget二选一传入
//示例功能详见hud界面左下角的聊天栏
public string GetShowText(string textContent,Text textComponent,float widthOriginal,float widthTarget = 0,string textTarget = "")
{
if (widthTarget == 0 && !textTarget.Equals(""))
{
widthTarget = GetWidth(textComponent, textTarget);
}
float tempWidth = 0;
string stringTemp = string.Empty;
string stringTarget = string.Empty;
Array tab = SplitString(textContent);
for (int i = 0; i < tab.Length - 1; i++)
{
stringTarget = stringTemp;
stringTemp = stringTemp + tab.GetValue(i);
tempWidth = GetWidth(textComponent, stringTemp);
if ((widthTarget + tempWidth) >= widthOriginal)
{
break;
}
}
if ((widthTarget + tempWidth) >= widthOriginal)
{
if (!textTarget.Equals(""))
{
stringTarget = stringTarget + textTarget;
}
}
else
{
stringTarget = stringTemp;
}
return stringTarget;
}
private void Start()
{
string sss = GetShowText("dasda这是一个例子にほんçai222!]]//*", this.GetComponent<Text>(), 100, 0 ,"...");
this.GetComponent<Text>().text = sss;
}②Lua&C#
//获取字符串宽度
public float GetWidth(string textContent)
{
Text text = this.GetComponent<Text>();
Font font = text.font;
if (font == null)
{
return 0;
}
int fontSize = font.fontSize;
FontStyle fontStyle = text.fontStyle;
font.RequestCharactersInTexture(textContent, fontSize, fontStyle);
CharacterInfo characterInfo;
float width = 0f;
for (int i = 0; i < textContent.Length; i++)
{
font.GetCharacterInfo(textContent[i], out characterInfo, fontSize);
width += characterInfo.advance;
}
return width;
}--[[
获取允许显示的文本内容local args = {textContent = '这是一个参数示例',textComponent = self.Text.chatTxt1,widthOriginal = 393,textTarget = '...'}
textContent:原文本内容
textComponent:text组件
widthOriginal:text组件节点的width(通过直接传入这个值,来避免在此方法内频繁调用GetComponent)
widthTarget:想要显示的区域宽度(比如省略号宽度10,那width就是10)和textTarget二选一传入
textTarget:想要添加的字符串(比如添加省略号'...')和widthTarget二选一传入
示例功能详见hud界面左下角的聊天栏
]]
function string.getShowText(args)
local widthTarget = args.widthTarget
if not args.widthTarget and args.textTarget then
widthTarget = args.textComponent:GetFontWidth(args.textTarget)
end
local tempWidth = 0
local stringTemp = ''
local stringTarget = ''
local tab = string.splitString(args.textContent)
for i=1,#tab do
stringTarget = stringTemp
stringTemp = stringTemp .. tab[i]
tempWidth = args.textComponent:GetFontWidth(stringTemp)
if (widthTarget + tempWidth) >= args.widthOriginal then
break
end
end
if (widthTarget + tempWidth) >= args.widthOriginal then
if args.textTarget then
stringTarget = stringTarget .. args.textTarget
end
else
stringTarget = stringTemp
end
return stringTarget
end
--[[
拆分字符串
]]
function string.splitString(s)
local tab = {}
local f = '[%z\1-\127\194-\244][\128-\191]*'
for v in string.gmatch(s,f) do
table.insert(tab , v)
end
return tab;
end边栏推荐
- scratch 编程 + 小学数学
- JSP Servlet JDBC MySQL CRUD 示例教程
- 用 Array.every & Array.some 匹配全部/部分内容 es6
- webUI测试框架设计思路详解
- 私域增长 | 私域会员:9大连锁行业15个案例集锦
- Kotlin - Coroutine Scope CoroutineScope, Coroutine Builder CoroutineBuilder, Coroutine Scope Function CoroutineScope Functiom
- Chrome——插件推荐
- Kubernetes:(四)常用命令
- 华为笔记本键盘锁住了(笔记本电脑键盘怎么亮起来)
- 程序员如何提升自己写代码的能力?
猜你喜欢

Verilog的时间格式系统任务----$printtimescale、$timeformat

磁性层状双金属氢氧化物和酶-DNA复合物|聚乙烯亚胺-DNA复合物(PEI/DNA)|作用机理

图床软件要收费,算了我自己写一个开源免费的。

全渠道电商 | 国内知名的药妆要如何抓住风口实现快速增长?

正则表达式

Hyaluronic acid-siRNA透明质酸修饰核糖核酸|peptide–siRNA 多肽偶连RNA/DNA核酸(齐岳PNA修饰物)

The second growth curve | The driving guide for corporate innovation to break through the stagnation dilemma

使用IDEA连接mysql

Build your own image search system (1): 10 lines of code to search images by image

【AutoSAR 十二 模式管理】
随机推荐
ESP8266-Arduino programming example-LittleFS and data upload
Data visualization ---- web page displays temperature and humidity
include用法及搭配(include相关短语)
使用IDEA连接mysql
ACM study book introduction
【体系结构 四 存储结构】
Kotlin - Coroutine Scope CoroutineScope, Coroutine Builder CoroutineBuilder, Coroutine Scope Function CoroutineScope Functiom
regular expression
C language learning books zero-based introductory articles
ds1302——Bin brother 51
点击返回顶部
Mass data query scheme mysql_Mysql massive data storage and solution 2 - Mysql sub-table query massive data... [easy to understand]
Single-core browser and what is the difference between dual-core browser, which to use?
用对象字面量或Map替代Switch/if语句
ACM学习书籍简介
全排列的一点小技巧:康托展开
找工作那些事-和表弟的一次聊天
Expert advice | How to formulate a growth strategy for survival in an economic downturn
【AutoSAR 十一 通信相关机制】
PEG-siRNA-PCL|siRNA-PEG-LHRH|MPEG-siRNA 甲氧基聚乙二醇修饰核酸