0

I have controls in my view. All objects have different width and height. I have an array of these objects. I need to write recursive function that will move to a new line my objects if they don't fit in the current line (something like in notepad or textedit).

I suppose we need to check current object from array in cycle and if it fits to width we don't do any operations and if it does not fit to width of current view (line) we should to make recursion. am I right? And move our object to new line while we have object in array.

4

1 回答 1

1

You can do it like this (assuming all starts at 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;
        }
    }
于 2012-10-30T14:13:28.233 回答