适合矩形的字符串
如何手动实现字符串分割算法以适合矩形。(在需要时插入...或\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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,假设仅左对齐,一个非常基本的方法是这样的:
您引入两个索引 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.
分割字符串(使用您选择的分隔符 - 我假设您希望它是空格),然后循环遍历字符串标记,打印每个标记,直到您无法将其放入剩余空间,然后转到下一个线并继续前进。
请参阅这篇文章了解如何拆分字符串。
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.
你没有提供任何背景信息,所以无法回答你的问题。根据您给我的内容,只需计算矩形的宽度,并将字符串的长度设置为矩形的宽度减去一些额外的插入空间。
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.