思路:
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]);
? ? ? ? ? ? //反轉(zhuǎn)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;
? ? ? ? }