Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27191

UILabel fit issues

I use this method for recalculate frame for my labels:

- (void)fitElements {    
    CGFloat currentX = 0.0;
    CGFloat currentY = 0.0;    
    for (UIView *view in elements) {    
        CGRect rect = view.frame;
        rect.origin.x = currentX;
        rect.origin.y = currentY;        
        currentX = rect.origin.x + rect.size.width + 5;        
        view.frame = rect;      
        if (currentX >= 420) {
            currentX = 0.0;
            currentY += rect.size.height + 5;
        }
    }
}

If my label crosses the border that more than 420 I move my object to next line.

- (void)createElements {
    NSInteger tag = 0;
    for (NSString *str in words) {
        UILabel *label = [[UILabel alloc] init];
        [self addGesture:label];
        [label setTextColor:[UIColor blueColor]];
        label.text = str;
        [label setAlpha:0.8];
        [label sizeToFit];
        [elements addObject:label];
    }
}

This is how it looks if I create object as above (using [label sizeToFit];)

enter image description here

as we can see all my label went out of border

but if I use label with hardcode frame I get this:

enter image description here

and this is what I want, but in this case I have static width of object.

This is my method with a hardcode frame.

- (void)createElements {
    NSInteger tag = 0;
    for (NSString *str in words) {
        UILabel *label = [[UILabel alloc] init];
        [self addGesture:label];
        [label setTextColor:[UIColor blueColor]];
        label.text = str;
        [label setAlpha:0.8];
        [label setFrame:CGRectMake(0, 0, 100, 20)];
        [elements addObject:label];
        tag++;
    }
}

How can I make object with relative width and that it also can recalculates correctly?

Upvotes: 1

Views: 177

Answers (1)

Rok Jarc
Rok Jarc

Reputation: 18875

You could achieve something like left-justify with small modification of your code:

- (void)fitElements {
CGFloat currentX = 0.0;
CGFloat currentY = 0.0;
for (UILabel *view in elements) { //UIView changed to UILabel
    CGRect rect = view.frame;
    rect.origin.x = currentX;
    rect.origin.y = currentY;
    rect.size.width = [self widthOfString: view.text withFont:view.font];
    currentX = rect.origin.x + rect.size.width + 5;
    view.frame = rect;
    if (currentX + rect.size.width >= 420) {   //EDIT done here
        currentX = 0.0;
        currentY += rect.size.height + 5;
        rect.origin.x = currentX;
        rect.origin.y = currentY;
        view.frame = rect;
        currentX = rect.origin.x + rect.size.width + 5;
    }
}}

- (CGFloat)widthOfString:(NSString *)string withFont:(NSFont *)font {
     NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
     return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width;
 }

widthOfString method is copied from Stephen's answer

EDIT:

You can also find a lot of useful methods dealing with size of graphical representation of a string in NSString UIKit Additions.

Upvotes: 2

Related Questions