我的 View 中有控件。所有对象都有不同的宽度和高度。我有一组这些对象。我需要编写递归函数,如果对象不适合当前行(类似于记事本或文本编辑器),它将移动到新行我的对象。
我想我们需要循环检查数组中的当前对象,如果它适合宽度我们不做任何操作,如果它不适合当前 View (行)的宽度我们应该进行递归。我对吗?当我们在数组中有对象时,将我们的对象移动到新行。
最佳答案
你可以这样做(假设所有都从 0.0 开始):
NSArray* your_elements = ... //your elements in an NSArray (labels, buttons any type of views)
CGFloat currentX = 0.0;
CGFloat currentY = 0.0;
CGFloat maxX = 320.0;
CGFloat lineHeight = 12.0;
for (UIView* some_view in your_elements) {
CGRect newframe = some_view.frame;
newframe.origin.x = currentX;
newframe.origin.y = currentY;
currentX += newframe.size.width;
some_view.frame = newframe;
if (currentX>maxX) {
currentX = 0.0;
currentY += lineHeight;
}
}
关于c++ - 创建文本编辑的递归函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13140134/