love.graphics.printf (简体中文)

绘制格式化过的文本,可折行显示、对齐。

参见 love.graphics.print.

The word wrap limit is applied before any scaling, rotation, and other coordinate transformations. Therefore the amount of text per line stays constant given the same wrap limit, even if the scale arguments change.

In version 0.9.2 and earlier, wrapping was implemented by breaking up words by spaces and putting them back together to make sure things fit nicely within the limit provided. However, due to the way this is done, extra spaces between words would end up missing when printed on the screen, and some lines could overflow past the provided wrap limit. In version 0.10.0 and newer this is no longer the case.

In versions prior to 11.0, color and byte component values were within the range of 0 to 255 instead of 0 to 1.

O.png Aligning does not work as one might expect! It doesn't align to the x/y coordinates given, but in a rectangle, where the limit is the width.  


O.png Text may appear blurry if it's rendered at non-integer pixel locations.  


方法

语法

love.graphics.printf( text, x, y, limit, align )

参数

string text
文本。
number x
文本起点处的x轴坐标。
number y
文本起点处的y轴坐标。
number limit
文本达到多少像素后折行显示。
AlignMode align ("left")
对齐方式。
Available since LÖVE 0.9.0
number r (0)
Orientation (radians).
number sx (1)
Scale factor (x-axis).
number sy (sx)
Scale factor (y-axis).
number ox (0)
Origin offset (x-axis).
number oy (0)
Origin offset (y-axis).
number kx (0)
Shearing factor (x-axis).
number ky (0)
Shearing factor (y-axis).

返回

无。

Function

Available since LÖVE 11.0
This variant is not supported in earlier versions.

Synopsis

love.graphics.printf( text, font, x, y, limit, align, r, sx, sy, ox, oy, kx, ky )

Arguments

string text
A text string.
Font font
The Font object to use.
number x
The position on the x-axis.
number y
The position on the y-axis.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.
number r (0)
Orientation (radians).
number sx (1)
Scale factor (x-axis).
number sy (sx)
Scale factor (y-axis).
number ox (0)
Origin offset (x-axis).
number oy (0)
Origin offset (y-axis).
number kx (0)
Shearing factor (x-axis).
number ky (0)
Shearing factor (y-axis).

Returns

Nothing.

Function

Available since LÖVE 11.0
This variant is not supported in earlier versions.

Synopsis

love.graphics.printf( text, transform, limit, align )

Arguments

string text
A text string.
Transform transform
Transformation object.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.

Returns

Nothing.

Function

Available since LÖVE 11.0
This variant is not supported in earlier versions.

Synopsis

love.graphics.printf( text, font, transform, limit, align )

Arguments

string text
A text string.
Font font
The Font object to use.
Transform transform
Transformation object.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.

Returns

Nothing.

Function

Available since LÖVE 0.10.0
This variant is not supported in earlier versions.

Synopsis

love.graphics.printf( coloredtext, x, y, limit, align, angle, sx, sy, ox, oy, kx, ky )

Arguments

table coloredtext
A table containing colors and strings to add to the object, in the form of {color1, string1, color2, string2, ...}.
table color1
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string1
A string of text which has a color specified by the previous color.
table color2
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string2
A string of text which has a color specified by the previous color.
tables and strings ...
Additional colors and strings.
number x
The position of the text (x-axis).
number y
The position of the text (y-axis).
number limit
The maximum width in pixels of the text before it gets automatically wrapped to a new line.
AlignMode align
The alignment of the text.
number angle (0)
Orientation (radians).
number sx (1)
Scale factor (x-axis).
number sy (sx)
Scale factor (y-axis).
number ox (0)
Origin offset (x-axis).
number oy (0)
Origin offset (y-axis).
number kx (0)
Shearing / skew factor (x-axis).
number ky (0)
Shearing / skew factor (y-axis).

Returns

Nothing.

Notes

The color set by love.graphics.setColor will be combined (multiplied) with the colors of the text.

Function

Available since LÖVE 11.0
This variant is not supported in earlier versions.

Synopsis

love.graphics.printf( coloredtext, font, x, y, limit, align, angle, sx, sy, ox, oy, kx, ky )

Arguments

table coloredtext
A table containing colors and strings to add to the object, in the form of {color1, string1, color2, string2, ...}.
table color1
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string1
A string of text which has a color specified by the previous color.
table color2
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string2
A string of text which has a color specified by the previous color.
tables and strings ...
Additional colors and strings.
Font font
The Font object to use.
number x
The position on the x-axis.
number y
The position on the y-axis.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.
number angle (0)
Orientation (radians).
number sx (1)
Scale factor (x-axis).
number sy (sx)
Scale factor (y-axis).
number ox (0)
Origin offset (x-axis).
number oy (0)
Origin offset (y-axis).
number kx (0)
Shearing factor (x-axis).
number ky (0)
Shearing factor (y-axis).

Returns

Nothing.

Notes

The color set by love.graphics.setColor will be combined (multiplied) with the colors of the text.

Function

Available since LÖVE 11.0
This variant is not supported in earlier versions.

Synopsis

love.graphics.printf( coloredtext, transform, limit, align )

Arguments

table coloredtext
A table containing colors and strings to add to the object, in the form of {color1, string1, color2, string2, ...}.
table color1
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string1
A string of text which has a color specified by the previous color.
table color2
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string2
A string of text which has a color specified by the previous color.
tables and strings ...
Additional colors and strings.
Transform transform
Transformation object.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.

Returns

Nothing.

Notes

The color set by love.graphics.setColor will be combined (multiplied) with the colors of the text.

Function

Available since LÖVE 11.0
This variant is not supported in earlier versions.

Synopsis

love.graphics.printf( coloredtext, font, transform, limit, align )

Arguments

table coloredtext
A table containing colors and strings to add to the object, in the form of {color1, string1, color2, string2, ...}.
table color1
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string1
A string of text which has a color specified by the previous color.
table color2
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string2
A string of text which has a color specified by the previous color.
tables and strings ...
Additional colors and strings.
Font font
The Font object to use.
Transform transform
Transformation object.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.

Returns

Nothing.

Notes

The color set by love.graphics.setColor will be combined (multiplied) with the colors of the text.

示例

在屏幕上绘制一段文本,右对齐,宽度限制为125。

love.graphics.printf("This text is aligned right, and wraps when it gets too big.", 25, 25, 125, "right")

注意

注意limit参数会影响到居中对齐和右对齐时的显示效果。

love.graphics.printf("This text is aligned center",100, 100, 200,"center") -- center your text around x = 200/2 + 100 = 200
love.graphics.printf("This text is aligned right",100, 100, 200,"right") -- align right to x = 100 + 200 = 300

另:显示中文及中文换行的方法如下:

function love.load()
	--加载中文字体
	font = love.graphics.newFont("wqy-microhei.ttc", 18)
	love.graphics.setFont(font)
end

function love.draw()
	love.graphics.printf("我 是 一 个 地 球 人 我 是 一 个 中 国 人 我 是 一 个 地 球 人 我 是 一 个 中 国 人 我 是 一 个 地 球 人 我 是 一 个 中 国 人", 100, 200)
	--字间要手动加空格。应该有办法让空格显示弱化,叫leading还是什么的,待研究
end

See Also



Other Languages