适合矩形的字符串

发布于 2024-09-30 19:23:49 字数 236 浏览 2 评论 0原文

如何手动实现字符串分割算法以适合矩形。(在需要时插入...或\n) 我可以计算字符串的长度(以像素为单位)。

我有标准的 std::string 和 包含左、上、宽、高的矩形结构 如何将字符串拆分为单词短语,其大小不大于给定大小的矩形

返回字符串的宽度(以像素为单位)

int GetStringWidth(std::string str)

how to implement manually string splitting algorithm to fit it into the rectangle.(insert ... or \n where needed)
i can calculate the length of the string in pixels.

i have standart std::string and
rect struct containing left, top ,width ,height
how to split string into words phrases that it size was not larger than rectangle of the given size

returns width of string in pixel

int GetStringWidth(std::string str)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

近箐 2024-10-07 19:23:49

好的,假设仅左对齐,一个非常基本的方法是这样的:

您引入两个索引 lineStart/lineEnd ,它们标记输入字符串中可能的文本行的开始和结束。然后,您循环遍历输入字符串中的所有单词,并针对每个单词检查是否可以将其添加到当前行而不超出矩形宽度。如果是,则相应增加lineEnd。如果没有,请将当前片段 [lineStart..lineEnd] 添加到结果中,然后将 lineStart/lineEnd 重置为当前单词的开头(这将是下一行的第一个单词)。

有一些边界情况需要考虑,包括(但可能不限于)单个单词可能比矩形宽度更宽的可能性;最后一行可能也需要显式处理。

Ok, assuming left justification only, a very basic approach would be this:

You introduce two indices lineStart/lineEnd which mark the start and end of a possible text line in your input string. You then loop over all words in the input string, and for each word you check if it could be added to the current line without exceeding the rectangle width. If yes, increase lineEnd accordingly. If not, add the current fragment [lineStart..lineEnd] to the result, then reset lineStart/lineEnd to the start of the current word (which will be the first word on the next line).

There are a couple of border cases to consider, including (but likely not limited to) the possibility that a single word may be wider than the rectangle's width; and the very last line probably needs explicit handling as well.

浊酒尽余欢 2024-10-07 19:23:49

分割字符串(使用您选择的分隔符 - 我假设您希望它是空格),然后循环遍历字符串标记,打印每个标记,直到您无法将其放入剩余空间,然后转到下一个线并继续前进。

请参阅这篇文章了解如何拆分字符串

Split the string (using the delimiter of your choice - I'm assuming you want it to be whitespace), then loop through the string tokens, printing each one until you can't fit it in the remaining space, then go to the next line and keep going.

See this post for how to split a string.

忘东忘西忘不掉你 2024-10-07 19:23:49

你没有提供任何背景信息,所以无法回答你的问题。根据您给我的内容,只需计算矩形的宽度,并将字符串的长度设置为矩形的宽度减去一些额外的插入空间。

You haven't given any background information, so it's impossible to answer your question. From what you've given me, just calculate the width of the rectangle, and set the length of the string to be the width of the rectangle minus some extra space for inset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文