开发过程中遇到一个需求,就是在聊天栏中,对于超出聊天框的文字需要省略并添加省略号,比如输入”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
今天的文章指定宽度截取字符串的函数_截取某个字符之前的字符串[通俗易懂]分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/86475.html