Tom H
Tom H

Reputation: 517

How to calculate the frame.size.width

In my case to calculate a UILabel frame.origin.x to make it on the right side with code below:

writerNameLabel.frame = CGRectMake(cell.frame.size.width - writerNameLabel.frame.size.width - 80.......)

NSLog(@" writerNameLabel frame width: %f",writerNameLabel.frame.size.width);
NSLog(@" cell frame width: %f",cell.frame.size.width);
NSLog(@" cell - writerNameLabel frame width: %f",cell.frame.size.width - writerNameLabel.frame.size.width);
NSLog(@" writerNameLabel frame x: %f",writerNameLabel.frame.origin.x);


2012-08-17 14:43:36.895 writerNameLabel frame width: 38.000000
2012-08-17 14:43:36.899 cell frame width: 320.000000
2012-08-17 14:43:36.902 cell - writerNameLabel frame width: 282.000000
2012-08-17 14:43:36.904 writerNameLabel frame x: 240.000000

----

2012-08-17 14:43:36.924 writerNameLabel frame width: 102.000000
2012-08-17 14:43:36.928 cell frame width: 320.000000
2012-08-17 14:43:36.931 cell - writerNameLabel frame width: 218.000000
2012-08-17 14:43:36.934 writerNameLabel frame x: 240.000000

The problem is that this

cell.frame.size.width - writerNameLabel.frame.size.width

part in my calculation doesn't work, it only return the value of

cell.frame.size.width - 80

, why? Did I do anything wrong?

Please advise. Thanks.

Upvotes: 1

Views: 310

Answers (4)

Sonny Parlin
Sonny Parlin

Reputation: 51

the frame's default value is {0, 0, 0, 0} by default

Upvotes: 1

Mert
Mert

Reputation: 6065

How do you initialize the writerNameLabel? You should use initWithFrame, otherwise it has size (0,0).

Just Info:

I assume you are using UITableViewCell because name of your variable is cell :) In that case, it would be better if you use cell.contentView.frame.size.width instead of cell.frame.size.width. So you can prevent overlapping writerNameLabel with accessory view of the cell. Easiest approach would be using the labels, which are already(by default) on UITableViewCell instance.

Upvotes: 0

user843910
user843910

Reputation:

Because the value of writerNameLabel.frame.size.width is 0 at that time.

Upvotes: 1

user529758
user529758

Reputation:

I suspect the problem is that you haven't initially set the label's frame thus, by default, it's { 0, 0, 0, 0}, that's why it's ineffective in the calculation.

Upvotes: 3

Related Questions