UI文本多行不均匀对齐 [英] UI Text multiline uneven alignment

查看:71
本文介绍了UI文本多行不均匀对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文本是一个带有未定义单词数的字符串.对齐方式应如下所示:

The text is a string with undefined number of words. The alignment should be something like this:

        The text is starting from here
    then when the line is over it
goes to the next line etc.

是否可以使用UI组件在Unity中以这种方式对齐文本?

Is it possible to align text this way in Unity using UI components?

推荐答案

不,没有内置方法.

请注意,Unity 不提供文本长度"功能-例如iOS .

Note that Unity DOES NOT offer "text length" functions - as for example iOS does.

我真的根本想不起来要找出包裹它的地方"的线条.也许您可以一次又一次地遍历每个字符,使字符串一次增加一个字符,当宽度停止增加时,就知道它已经换行了. (使用.renderer.bounds.size.x获取宽度)

I really cannot think of anyway at all to find out "where it wraps" a line. You could, perhaps, iterate over each character one by one growing the string one character at a time, and when the width stops increasing, you know, it has wrapped. (Get the width with .renderer.bounds.size.x )

我可能会鼓励您只说三个UI.Text并分隔您的字符串.只需将字符串分开,说出50个字符的块(在一个空格处停止)或七个单词. (它们的长度不会完全相同,但是可以.)

I would probably encourage you to just have say three UI.Text and separate your string. Simply, separate the string in to say chunks of 50 characters (stopping at a space) or perhaps seven words. (They won't be exactly the same length, but it will be fine.)

注意

如果您是新的Unity程序员或业余爱好者,通常的解决方法是

If you're a new Unity programmer or hobbyist, the usual solution to things like this is to

使用资产!"

某人可能已经编写了您所需的程序.因此,开始搜寻诸如自由资产,将文字环绕形状"之类的内容.通常,向制作此类软件包的人员发送电子邮件是很有意义的,如果他们的软件包不需要,他们通常会知道可以满足您需求的东西.

Someone somewhere has probably programmed what you need. So start googling for something like "free asset, wrap text around a shape" or similar. Often, it pays to email the people who make such packages, and they often know something that does what you need, if their one does not.

示例.. http://forum.unity3d.com/threads/text- box.124906/

通过搜索unity3d text utility wrap around a shape

这是一些代码,用于将合理文本的长行分割为长度限制为50个字符的多行.请注意,文本必须是合理的",您不能有任何长得离谱的单词等.

Here's some code to split a LONG line of reasonable text, in to a number of lines of length limit say 50 characters. Note that the text must be "reasonable", you can't have any ridiculously long words etc.

string wholeSentence = "Your whole sentence here ... goes on and on.";

List<string> words = new List<string>(
  wholeSentence
  .Split(new string[] { " " },
  StringSplitOptions.RemoveEmptyEntries)
  );

List<string> finalLines = new List<string>();

int count = 0;
string nextLine = "";
foreach (word w in words)
  {
  nextLine = nextLine + w + " ";
  count += w.Length;
  if (count>50)
   {
   finalLines.Add(nextLine);
   count=0;
   nextLine = "";
   }
  }

if (nextLine!="") finalLine.Add(nextLine);

这将为您提供所有行,在List"finalLines"中!干杯

that will give you all the lines, in the List "finalLines" ! Cheers

这篇关于UI文本多行不均匀对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆