- /// <summary>
- /// 根据label的长度截断字符串
- /// </summary>
- /// <param name="text">文本内容</param>
- /// <returns></returns>
- private string CutText(string text)
- {
- text = "中文英文混排absddd中aad文英文混排 ABdc11231";
- if (!string.IsNullOrEmpty(text))
- {
- SizeF layoutSize = new SizeF(this._showlabel.Width, 10);
- // Measure string.
- int charactersFitted;
- int linesFilled;
- StringFormat stringFormat = new StringFormat();
- stringFormat.LineAlignment = StringAlignment.Far;
- //SizeF stringSize = new SizeF();
- using (Graphics g = this.CreateGraphics())
- {
- g.MeasureString(text, this._showlabel.Font, layoutSize, stringFormat, out charactersFitted, out linesFilled);
- }
- if (charactersFitted - 3 > 0)
- {
- //截取字符,包括中文和英文哦
- return Substring(text, charactersFitted);
- }
- }
- //this._showlabel.Font
- return text;
- }
- private string Substring(string str, int chatCount)
- {
- if (chatCount < str.Length)
- {
- if ((int)str[chatCount - 1] > 128)
- {
- chatCount = chatCount - 2;
- }
- else
- {
- if ((int)str[chatCount - 2] > 128)
- {
- chatCount = chatCount - 2;
- }
- else
- {
- chatCount = chatCount - 3;
- }
- }
- return str.Substring(0, chatCount) + "...";
- }
- else
- {
- return str;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0709201513637.html
来源: http://www.codesnippet.cn/detail/0709201513637.html