public class UILabelUtils
{
public static Vector3 CalculateSinglePosition(List<Vector3> verts, List<int> indices, string finalText, int index, int offset = 0)
{
index = indices.IndexOf(index);
index = index + offset;
Vector3 p1;
if (index != -1 && verts.Count > index * 2 + 1)
{
p1 = (verts[index * 2] + verts[index * 2 + 1]) * 0.5f;
}
else
{
p1 = (verts[verts.Count - 1] + verts[verts.Count - 2]) * 0.5f;
}
return p1;
}
public static Vector3 CalculateSinglePosition(UILabel label, string text, int index, int offset = 0)
{
List<Vector3> verts = null;
List<int> indices = null;
string finalText = UpdateCharacterPosition(label, text, out verts, out indices);
return CalculateSinglePosition(verts, indices, finalText, index, offset);
}
public static string UpdateCharacterPosition(UILabel label, string text, out List<Vector3> verts, out List<int> indices)
{
label.UpdateNGUIText();
List<Vector3> mTempVerts = new List<Vector3>();
List<int> mTempIndices = new List<int>(); ;
NGUIText.Update(false);
NGUIText.rectHeight = 1000000;
string mProcessedText = "";
// Wrap the text
bool fits = NGUIText.WrapText(text, out mProcessedText, false);
NGUIText.PrintExactCharacterPositions(mProcessedText, mTempVerts, mTempIndices);
verts = mTempVerts;
indices = mTempIndices;
return mProcessedText;
}
}
其中:verts.Count = indices.Count * 2 ,verts分别记录每个字符左上角和右上角的坐标。 其实verts,indices只记录了显示的字符位置及其字符的索引位置,富文本不在其中。 finalText包含了所有的字符包括不可见的。
网友评论