private void SetVisibleText() { _displayText = ""; if (Label.MeasureLine(_text) >= _clientAreaMain.Width) //Text wider than box. { if (_caretIndex < _displayIndex) { //Caret outside to the left. Move display text to the left by setting its index to the caret. _displayIndex = _caretIndex; } int glyphCount = 0; while (_displayIndex + (glyphCount + 1) < _text.Length && Label.MeasureLine(Text.Substring(_displayIndex + 1, glyphCount + 1)) < _clientAreaMain.Width) { glyphCount++; //How many glyphs we could/would draw with the current index. } if (_caretIndex > _displayIndex + glyphCount) //Caret outside? { if (_text.Substring(_displayIndex + 1).Length != glyphCount) //Still stuff outside the screen? { _displayIndex++; //Increase display index by one since the carret is one outside to the right. But only if there's still letters to the right. glyphCount = 0; //Update glyphcount with new index. while (_displayIndex + (glyphCount + 1) < _text.Length && Label.MeasureLine(Text.Substring(_displayIndex + 1, glyphCount + 1)) < _clientAreaMain.Width) { glyphCount++; } } } _displayText = Text.Substring(_displayIndex + 1, glyphCount); _caretPos = Label.Position.X + Label.MeasureLine(Text.Substring(_displayIndex, _caretIndex - _displayIndex)); } else //Text fits completely inside box. { _displayIndex = 0; _displayText = Text; if (Text.Length <= _caretIndex - 1) { _caretIndex = Text.Length; } _caretPos = Label.MeasureLine(Text.Substring(_displayIndex, _caretIndex - _displayIndex)); } }
private void TextStatus() { var FontSize = txtspr.Size; Console.WriteLine("Size Method: Width: " + FontSize.X + "px Height: " + FontSize.Y + "px"); var s = txtspr.MeasureLine(txtspr.Text); Console.WriteLine("MeasureLine Method: Width: " + s + "px"); s = txtspr.MeasureLine("Foo Bar is a Bar Foo... ! I hate Foo Bars!"); Console.WriteLine("MeasureLine Method: Width: " + s + "px"); }
public SizeF GetTextSize(string text, string fontName, float fontSize, Point pDest, bool wordWrap, Rectangle rectDest) { try { GorgonLibrary.Graphics.Font textFont = GetGorgonFont(fontName, fontSize); if (textFont != null) { textSprite.Font = textFont; } else { if (fontName.Equals("DEFAULT")) { textFont = new GorgonLibrary.Graphics.Font(fontName + "_" + fontSize, new System.Drawing.Font(SystemFonts.DefaultFont.FontFamily, fontSize)); } else { textFont = new GorgonLibrary.Graphics.Font(fontName + "_" + fontSize, new System.Drawing.Font(fontName, fontSize)); } textSprite.Font = textFont; } textSprite.Bounds = null; textSprite.WordWrap = false; textSprite.Text = text; textSprite.SetAxis((int)((float)rectDest.Width) / 2, (int)((float)rectDest.Height) / 2); textSprite.Rotation = 0; if (wordWrap == false) { textSprite.Position = new Vector2D(pDest.X, pDest.Y); } else { textSprite.SetPosition(rectDest.X + textSprite.Axis.X, rectDest.Y + textSprite.Axis.Y); textSprite.Bounds = new Viewport(0, 0, rectDest.Width, 0); } textSprite.WordWrap = wordWrap; //Get the max width of all lines float maxLineWidth = 0; for (int i = 0; i < textSprite.LineCount; i++) { float lineWidth = textSprite.MeasureLine(i); if (lineWidth > maxLineWidth) { maxLineWidth = lineWidth; } } float finalWidth = rectDest.Width; if (finalWidth < maxLineWidth) { finalWidth = maxLineWidth; } return(new SizeF(finalWidth, textSprite.Height)); } catch (Exception ex) { return(new SizeF(0, 0)); } }