| | |
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 根据文本,计算它实际的宽度(返回的是真实值)
|
| | | /// 根据文本,计算它实际的宽度
|
| | | /// </summary>
|
| | | /// <param name="textSize">字体大小,省略时使用当前控件的字体大小</param>
|
| | | /// <param name="i_text">需要计算的文本信息,省略时使用当前控件的文本</param>
|
| | | /// <returns></returns>
|
| | | public int GetRealWidthByText(float textSize, string i_text = null)
|
| | | public int GetRealWidthByText()
|
| | | {
|
| | | if (i_text == null)
|
| | | {
|
| | | i_text = this.Text;
|
| | | }
|
| | | if (textSize == -1)
|
| | | {
|
| | | textSize = this.TextSize;
|
| | | }
|
| | |
|
| | | int byteLength = 0;
|
| | | for (int i = 0; i < i_text.Length; i++)
|
| | | {
|
| | | byteLength += Encoding.UTF8.GetBytes(i_text[i].ToString()).Length;
|
| | | //int length = Encoding.UTF8.GetBytes(i_text[i].ToString()).Length;
|
| | | //if (length == 1)
|
| | | //{
|
| | | // //英文
|
| | | // byteLength += length;
|
| | | // continue;
|
| | | //}
|
| | | ////中文(暂时用中文对应)
|
| | | //byteLength += Encoding.GetEncoding("gb2312").GetBytes(i_text[i].ToString()).Length;
|
| | | }
|
| | | int realWidth = byteLength * (int)textSize;
|
| | | return Application.GetRealWidth(realWidth + 20);
|
| | | #if Android
|
| | | //需要增加一个误差值
|
| | | return this.GetTextWidth() + Application.GetRealWidth(12);
|
| | | #endif
|
| | | #if iOS
|
| | | //需要增加一个误差值
|
| | | return this.GetTextWidth() + Application.GetRealWidth(25);
|
| | | #endif
|
| | | }
|
| | |
|
| | | #endregion
|