Tutorial:Fonts and Text (简体中文)
在 LÖVE 中添加和使用字体并不是很复杂，但是，需要先设置好字体才能使用。
- 默认字体 （Bitstream Vera Sans）
- FreeType 2 支持的任意字体文件
- 包含字形的图像 （ImageFont）
font = love.graphics.newFont(14) -- 14 表示字体的大小
Font files are just normal files that you download from the internet (or create yourself) that contains font information. They can be used just like the default font, just that you specify the filepath along with the font size:
font = love.graphics.newFont("AwesomeFont.ttf", 15)
Keep in mind the that some fonts you download come with restrictions on when/how you can use them.
An image font is a little tougher to explain, as it all depends on a specifically formatted image. If you look at this you will see that it contains glyphs along one row, each separated by a specific color. This color is used to separate the glyphs and needs to be found at between every character and at the beginning of the list (all the way to the left). Creating a font based on this image would be as follows:
font = love.graphics.newImageFont("imagefont.png", " abcdefghijklmnopqrstuvwxyz" .. "ABCDEFGHIJKLMNOPQRSTUVWXYZ0" .. "123456789.,!?-+/():;%&`'*#=\"")
Warning: Make sure that the image starts and also ends with the separation color or else the last character (in this case " ) won't work.
Now that you have created your font object, you need to set it as the current font before it can be used. That is done with the following code:
-- font = the font object you made before love.graphics.setFont(font)
One can also immediately use a TTF-font by calling
Or, in the case of the default font,
Drawing and formatting text
So now that you have your wonderful new font object, you want to use it for something. To draw text, you simply call the
love.graphics.print("This is some awesome text", 100, 100)
You can easily draw numbers as well:
num = 15 love.graphics.print(num, 100, 150)
The default text rendering function can handle line breaks (the '\n' character) and will draw text aligned to the left:
love.graphics.print("This is the first line\nThis is the second one.\n(and a third)\n\nfifth", 100, 200)
The parameters '100' and '200' are the x- and y-locations of the text, respectively. In LÖVE versions prior to 0.6.0,
love.graphics.draw was instead used to draw text.
However, LÖVE can also draw text with word wrapping and alignment by using the more complicated
love.graphics.printf("A really long sentence which will probably be broken into many pieces by my love.", 500, 100, 250, 'right')
Where, here, the parameters '500' and '100' are x- and y-locations, '250' is the maximum text width, and 'right' is the text alignment.
This way it is relatively easy to draw completely centered text if you pass the width of the window as the word-wrap limit and 0 as the x-position:
love.graphics.printf("This text will be centered no matter what.\n(and you LOVE it)", 0, 400, 800, 'center')
Text will always align to the left by default if you omit the alignment value:
love.graphics.printf("Aligned to the left, yes very much so.", 100, 500, 150)
That is how to draw text. For more information about the specific functions and their use, please visit the documentation.
Note: All fonts (even the image font) will be affected by the current color, there is no need to set the color mode to 'modulate'.
Tiếng Việt –