The approach is simple in concept, a bit involved in execution. It involves only a few steps:
Split the text into lines that will fit horizontally
Compute the vertical position of the first line
for each line
Compute its width and the X position
Display it at the Y position
Add the line height to the current Y position
关键部分是第一步。 其余部分比较容易。
If you have a fixed-width font, then splitting the text into lines isn t too hard. You simply compute how many characters will fit in the given width, index into the string to that position, and then back up to the previous word break. Grab the substring from the start of the previous line (or start of the string for the first line) to that position, and that s your line. Repeat until you get to the end of the string.
有了变形的字体,事情就更加困难,因为你可以只将字面列入<条码>* 的性质——带宽/代码。 相反,你们不得不猜测、测试、精炼gues等。 每一图形子系统一号都使用某种<代码>MeasureString方法,这种方法将告诉我,鉴于某一个字体,它要做些什么样子。 有鉴于此,我过去所做的是:
Divide the surface width by the font s average character width
Index that far into the string, and find the next (or previous) word break.
Measure the string
If the result is wider than the width, go back one word and measure again.
If the result is narrower than the width, go forward one word and measure again.
Repeat the measure/adjust until you find the string that will fit.
一旦找到了适合的子体,将你的开端指数推向下线的开端,并在你到达地势结束之前再次这样做。
Vertically centering the group of lines:
starting_position.Y = (Surface_height - (num_lines * line_height)) / 2
横向集中一条线很容易实现:
starting_position.X = (Surface_width - Measured_string_width) / 2
You have to compute the starting_position.X
for each line. The Y coordinate is increased by line_height
for each successive line.