Then I use wxDC::DrawLabel for drawing multiline text I want to control font leading (linespacing). Is it possible?
There is no existing function to do that.
You can create your own. Use DrawText() to draw the individual lines with the spacing you want, but you draw each line individually. Use GetTextExtent() to find the height of the lines, and hence how much linespacing you'll need.
Related
I've been trying to find a way to change a font file's line spacing default value, using QFont, QFontMetrics or something like that. I'm using QPainter::drawText to draw some text in a bounding rectangle.
It's strange that QFont allows for font kerning to be changed and even has some stretch operation and letter spacing but nothing to change the default space between lines. I've searched and found some partial solutions using QTextLayout but none seemed to work properly.
I need to use QPainter because I generate a texture with the text to be rendered with OpenGL.
Looking for more ideas for me to try out!
UPDATE
I've found that I can use QPainter to draw a QStaticText which allows for HTML text formatting, similar to QTextDocument. However, CSS styling doesn't work like in QTextDocument (there's a bug report)... Therefore still no leading but I hope this puts me on the right track.
SOLVED
I got what I wanted using QTextDocument, like Mykhaylo suggested. Link to solution
QFontMetrics was not designed particularly for multi-line text.
Use QTextDocument. You can print multi-line and rich text with it, even using QPainter. See the solution how to use QPainter with QTextDocument
It seems there is not much that can be done here.
QFontMetrics::lineSpacing returns what you need but it is read-only.
It's the sum of font height and leading. You can adjust height - set it in QFont constructor. But you can't set leading.
Some people add \n to the end of string to increase space between lines but of course this is not always a good solution.
I use cocos2d:::Label and I need to control the spacing of adjustment characters in the text. I use cocos2d-x 3.0 and the font of the label is TTF. Is it possible to achieve, or I should use another thing instead of cocos2d:::Label?
I figured out that bitmap fonts can be created with appropriate inter-character spacing. There is a good online tool for that. And it can customize even more settings, and it is very handy.
How can one make a CStatic with text auto-wrap (multiline) which vertically centeres the result in the control's rectangle?
The problem I'm trying to solve is this: I have a CStatic control next to a CComboBox which updates information text depending on the choice. This text can be either short or long, requiring the CStatic to sometimes use multi-lines, and sometimes not. I want the info-text be vertically center-aligned with the CComboBox.
Now here is the problem:
If I make the CStatic only 1 textline high, it looks good for 1-line texts, but multi-lines do not fit and are not displayed.
If I make the CStatic higher to fit 2 lines, it looks good for long texts (with 2 lines), but 1-line-texts are shifted upwards, as the CStatic aligns the text on the top. A CStatic with the behavior mentioned in the question would solve this...
If I can't easily get a vertically centered CStatic multi-line control, the alternative would be to resize the control rect depending on the amount of text in it. But in this case I have a different problem:
How can I programatically find out how many lines a text will need in a CStatic of specific width?
Unfortunately you can't vcenter multi-line text in a CStatic.
Your next question has a solution but it's a bit of a pain to use. What you do is you use CDC::DrawTextEx with the DT_CALCRECT flag to get the size (in pixels) of the text you want to format. By dividing that by the height of a line of text (given in the font info you can get from the DC, plus some spacing which I'm not sure of how much that is - presumably it's a fixed amount, I don't think you can specify line spacing with DrawText), you will get (an approximation of) the number of lines you will get. You can then resize the control rect.
Come to think of it, you are probably better off not converting to lines and just resize your control to the extent you get from DrawTextEx :)
Things like this usually require some experimentation to get exactly right, and sometimes behave differently between OS versions. Proceed with caution.
I am using SDL and libfreetype and constructing a very basic GUI, I'm implementing a textBox, but I haven't been able to figure out how I'm supposed to generate the standard blinking cursor part. It doesn't seem to be exactly the same as a | character. And moreover if I draw it as a | character that changes the text width.
What's the canonically correct way to render text in a textbox with a cursor?
The easiest way is to just draw a line primitive, this gives you a lot more control over the spacing, length and width of the caret.
And if you want to keep it as a text character in your font system, you can do a render-to-texture and copy it out, or do a simple memory blit onto your font atlas (so you can can keep pipe character separate, an use a control char like 0x01 for the caret).
I find have used both these functions before, but I don't quite see the the difference between them. Well, I know that DrawText requires a formatting rectangle,and can do some text formatting, and textout only the starting coordinates, are there any other differences?
DrawText
It draws a text string into a rectangle region specified in logical coordinates.
It provides convenient ways of formatting multiline text.
It is mainly used for
wordbreaking paragraph formatting, expanding tabs etc.
TextOut
It is a simple text-drawing function which is easy to use.
It draws a character string at a specified location, using the currently selected text attributes.
The text
string to draw does not need to be zero terminated.
Also, take a look at ExtTextOut and DrawTextEx
DrawText() is User32.dll
TextOut() is Gdi32.dll
DrawText most likely calls TextOut in its implementation.
Draw text can be used to just give the length or size of text without actually displaying it. This is useful when you have to fine the maximum display length of a set of strings. Also if you supply a null terminated string as the input in DrawText, it is not necessary to supply the length of the string - that is automatically created.
Take a look at this and this.