思路:
1,将要显示的文本由前向后拆分为若干列;
2,然后由每一列组成要显示的每一行;
3,给每一行增加换行后拼接成一个正向显示的字符串;
4,将拼接后的字符串直接赋值给UILabel(还是按照从左往右显示)
////// 获得从右向左的竖排文字
///UILabel空间,主要用于获取字号和行间距
///原始字符串
///限定的文本高度 ///
public static string GetVerticalBeginFromRight(UILabel label, string str, int heightLim, string spaceXStr) {
string retStr = string.Empty;
if (null == label || str.Equals(string.Empty)) { return ""; }
int fontSize = label.fontSize;
int spacingY = label.spacingY; //首先计算能够有多少列 int lineNum = heightLim/(fontSize + spacingY);
int column = (int)Mathf.Ceil( (float)str.Length / lineNum ); //Debug.LogError("================lineNum:" + lineNum + "==column:" + column); ListcolStrs = new List(column);
int lastListIndex = 0;
int listIndex = 0;
for ( int i = 0;i < str.Length;i++)
{
listIndex = i / lineNum;
if(listIndex > lastListIndex)
{
string colStr = str.Substring(lastListIndex * lineNum, lastListIndex * lineNum + lineNum > str.Length ? str.Length : lineNum);
colStrs.Add(colStr);
//Debug.LogError("================lastListIndex:"+ lastListIndex + "==colStrs[lastListIndex]:" + colStrs[lastListIndex]);
}
lastListIndex = listIndex;
}
string nextColStr = str.Substring(listIndex * lineNum, listIndex * lineNum + lineNum > str.Length ? str.Length - listIndex * lineNum : lineNum);
colStrs.Add(nextColStr);
//Debug.LogError("================listIndex:" + listIndex + "==colStrs[listIndex]:" + colStrs[listIndex]);
//反转colStrs
colStrs.Reverse();
//拼接从左向右的换行字符串
for( int i = 0;i < lineNum;i++)
{
//获取每一列中的字符
string tempStr = string.Empty;
for ( int m = 0;m < column; m++)
{
string lineChar = colStrs[m].Length > i ? colStrs[m].Substring(i, 1) : GenBlank(CaculateBlankNeed(fontSize, fontSize));
//获得符号的宽度
int charW = GetTextPrintedSize(lineChar, fontSize);
if( charW >= fontSize)
{
tempStr += (spaceXStr + lineChar);
}
else
{
string blank = GenBlank(CaculateBlankNeed(fontSize - charW, fontSize));
tempStr += (blank + spaceXStr + lineChar);
}
}
retStr += tempStr + '\n';
}
//Debug.LogError("=================retStr:\n" + retStr);
label.text = retStr;
return retStr;
}
网友评论