Raccoon
Raccoon

Reputation: 1427

Dynamic UILabel positions using interface builder?

I'm trying to make a dynamic view with multiple numbers of UIlabels (and possibly image) However, I'm having troubles with positioning them dynamically. The following is what it should look like...

Date (UILabel)
Title (UILabel)
Image (UIImage)
Body Text (UILabel)

You can see that the height of image and the length of the body text can vary. If the image is huge, it must push the body text downward so that they don't get overlapped each other...

Is it possible to be done using only Interface Builder? If not, how can I programmatically do it? - by manually calculating each UI's height? Some sample codes would be great.... Thank you.

Upvotes: 0

Views: 442

Answers (1)

RolandasR
RolandasR

Reputation: 3046

You can't its only available for MacOS at the moment. You will have to do it in code.

I'm using such category:

@interface UILabel (UDSize)

- (void)sizeHeightToFitText;

@end

@implementation UILabel (UDSize)

- (void)sizeHeightToFitText {
    [self setLineBreakMode:UILineBreakModeWordWrap];
    [self setNumberOfLines:0];

    CGSize expectedSize = [[self text] sizeWithFont: self.font
                                  constrainedToSize: CGSizeMake(self.frame.size.width, CGFLOAT_MAX)
                                      lineBreakMode: self.lineBreakMode];

    CGRect newFrame = self.frame;
    newFrame.size.height = expectedSize.height;
    [self setFrame: newFrame];
}

@end

Upvotes: 1

Related Questions