void DisplayText() { float lineLenNow = FirstLineOffset; int lastStrLine = 0; for (int i = 0; i < HyperStrs.Count; i++) { if (lastStrLine != HyperStrs[i].line) { lastStrLine = HyperStrs[i].line; lineLenNow = 0; } HyperString hyperStr = HyperStrs[i]; float heightSpace = AlignWay == AlignType.Bottom ? HeightSpace * (MaxLine - hyperStr.line) : -HeightSpace * hyperStr.line; if (hyperStr.type == HyperType.PureString || hyperStr.type == HyperType.Clickable) { UILabel lbl = GetLabel(); lbl.text = hyperStr.content; lbl.transform.localPosition = new Vector3(lineLenNow, heightSpace, lbl.transform.localPosition.z); lineLenNow += hyperStr.length; if (hyperStr.type == HyperType.Clickable) { SetClickWidget(lbl, hyperStr.length); mClickableDic.Add(lbl.gameObject, hyperStr); } } else if (hyperStr.type == HyperType.Sprite) { UISprite spr = GetSprite(); spr.spriteName = hyperStr.content; if (hyperStr.inputInfo != string.Empty) { spr.Dimensions = new Vector2(hyperStr.length, spr.Dimensions.y); } else { spr.Dimensions = SprPrefab.Dimensions; } Vector3 sprSize = spr.Dimensions; spr.transform.localPosition = new Vector3(lineLenNow, heightSpace, spr.transform.localPosition.z); lineLenNow += hyperStr.length; } else if (hyperStr.type == HyperType.Voice) { UIVoiceChat voiChat = GetVoiceChat(); voiChat.TimeLabel.text = hyperStr.content; voiChat.transform.localPosition = new Vector3(lineLenNow, heightSpace, voiChat.transform.localPosition.z); lineLenNow += hyperStr.length; SetClickWidget(voiChat, hyperStr.length); mClickableDic.Add(voiChat.gameObject, hyperStr); } } }
Vector2 GetHyperStrLength(HyperString hyperStr) { Vector2 conLen = Vector2.zero; if (hyperStr.type == HyperType.Sprite) { if (hyperStr.inputInfo != string.Empty) { conLen.x = int.Parse(hyperStr.inputInfo); } else { Vector3 sprSize = SprPrefab.Dimensions; conLen.x = sprSize.x; } hyperStr.length = conLen.x; } else if (hyperStr.type == HyperType.PureString || hyperStr.type == HyperType.Clickable) { conLen = NGUIText.CalculatePrintedSize(hyperStr.content, LblPrefab); conLen = new Vector2(conLen.x + ElementSpace, conLen.y); hyperStr.length = conLen.x; } else if (hyperStr.type == HyperType.Voice) { if (VoiPrefab == null) { Debug.Log(gameObject.name); } Vector3 sprSize = VoiPrefab.BackgroundSprite.Dimensions; conLen.x = sprSize.x; hyperStr.length = conLen.x; } return(conLen); }
void CalcuTextsLengthAndLine() { int lineNumNow = 0; float oneLenLeft = LenLimit - FirstLineOffset; int hitLineLimitIndex = -1; for (int i = 0, iMax = HyperStrs.Count; i < iMax; i++) { if (LineLimit > 0 && lineNumNow > LineLimit) { hitLineLimitIndex = i; break; } string content = HyperStrs[i].content; if (content == "\n") { oneLenLeft = LenLimit; lineNumNow++; continue; } Vector2 conLen = GetHyperStrLength(HyperStrs[i]); if (conLen.x <= oneLenLeft || conLen.x - ElementSpace <= oneLenLeft) { //set this line now,can short the length left in this line oneLenLeft -= conLen.x; HyperStrs[i].line = lineNumNow; } else { //this content is cross tow line if (HyperStrs[i].type == HyperType.PureString) { //pure string will cut self to twzo new string,old str remain this line,new str insert to list,then check next routine HyperString purStr = HyperStrs[i]; int changeLineIndex = NGUIText.GetCharIndexFitLength(content, oneLenLeft, LblPrefab); string remainStr = content.Substring(0, changeLineIndex); string newStr = content.Substring(changeLineIndex, content.Length - changeLineIndex); RepairStringColor(ref remainStr, ref newStr); HyperStrs[i].line = lineNumNow; HyperStrs[i].content = remainStr; HyperStrs.Insert(i + 1, new HyperString(HyperType.PureString, newStr)); iMax = HyperStrs.Count; oneLenLeft = LenLimit; } else { //otherwise,move to next line HyperStrs[i].line = lineNumNow + 1; oneLenLeft = LenLimit; oneLenLeft -= conLen.x; } lineNumNow++; } } if (hitLineLimitIndex > 0) { HyperStrs.RemoveRange(hitLineLimitIndex, HyperStrs.Count - hitLineLimitIndex - 1); } MaxLine = lineNumNow; }
void DecodeText(string txt) { StringBuilder strB = new StringBuilder(); for (int i = 0, imax = txt.Length; i < imax; ++i) { char c = txt[i]; if (c == '\n') { if (strB.Length > 0) { string content = strB.ToString(); HyperStrs.Add(new HyperString(HyperType.PureString, content)); strB.Length = 0; } HyperStrs.Add(new HyperString(HyperType.PureString, "\n")); continue; } // Skip invalid characters if (c < ' ') { continue; } //hit hyper text if (c == '{') { //save previous pure str if (strB.Length > 0) { string content = strB.ToString(); HyperStrs.Add(new HyperString(HyperType.PureString, content)); strB.Length = 0; } //decode one hyper text if (i + 1 < imax) { //first char is the type int type = int.Parse(txt[i + 1].ToString()); HyperString hyperS = new HyperString((HyperType)type); i += 2; //get the content before we hit first ',' StringBuilder contentS = new StringBuilder(); for (; i < imax; i++) { if (txt[i] == ',') { //get Id before we hit first '}' StringBuilder idS = new StringBuilder(); i++; for (; i < imax; i++) { if (txt[i] == '}') { hyperS.inputInfo = idS.ToString(); break; } idS.Append(txt[i]); } } if (txt[i] == '}') { string content = contentS.ToString(); hyperS.content = content; HyperStrs.Add(hyperS); break; } contentS.Append(txt[i]); } } } else { strB.Append(c); } } if (strB.Length > 0) { string content = strB.ToString(); HyperStrs.Add(new HyperString(HyperType.PureString, content)); } }