iPhone Programmatically
iPhone Programmatically

Reputation: 1227

Automatic Spacing between labels

How can we set automatic space between labels. The dynamic text is provided to the labels, and second label should auto frame itself, so that both are not over written. How can i achieve this. I need when first label text finish then second label starts keeping distance from previous. Please guide for the above. Thanks in advance.

Upvotes: 0

Views: 65

Answers (2)

Aman Aggarwal
Aman Aggarwal

Reputation: 3754

Try this

CGSize size = [firstLabel.text sizeWithFont:[UIFont boldSystemFontOfSize:14.0]];
secondLabel.frame=CGRectMake(size.width+20,4, 9, 14);

For more info, chk this Get length of a UIlable according to a string of diffrent length

For getting image size

UIImage *tempimage= [UIImage imageWithContentsOfFile:fullImgNm];
Float width =   tempimage.size.width;

so your imageview frame will be

yourImageView.frame=CGRectMake(size.width+20,4,width, 14);

and your secondlabel frame will be

secondLabel.frame=CGRectMake(size.width+width+20,4, 9, 14);

Upvotes: 1

Madhu
Madhu

Reputation: 1542

Adjust the UILabel frame using constrainedToSize propery .

CGRect infoRect = self.conditionLabel.frame;
NSString *fileName = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"txt"]; 

NSString *textString = [[NSString alloc] initWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];
[self.conditionLabel setText:textString];
[textString release];

CGSize defaultSize = CGSizeMake(sectionWidth, 9999.0);
UIFont *defaultFont = kCCOpenSansFont(font, 14.0);

CGSize infoSize = [[self.conditionLabel text] sizeWithFont:defaultFont constrainedToSize:defaultSize lineBreakMode:UILineBreakModeWordWrap];

infoRect.size.height = infoSize.height;
[self.conditionLabel setFrame:infoRect];

Upvotes: 0

Related Questions